From b410b31c70fa2add7274eb4104538a67e5914d24 Mon Sep 17 00:00:00 2001 From: Vladimir Radosavljevic Date: Tue, 18 Jun 2024 15:46:06 +0200 Subject: [PATCH 1/2] [InstCombine] Add pre-commit test for Fix miscompile on GEP+load to icmp fold Add test where for folding GEP+load to icmp (uint64_t)-1 is used instead of -1 that is represented in 256 bits. Signed-off-by: Vladimir Radosavljevic --- .../Transforms/InstCombine/load-cmp-bug.ll | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/load-cmp-bug.ll diff --git a/llvm/test/Transforms/InstCombine/load-cmp-bug.ll b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll new file mode 100644 index 000000000000..f4605fa5073e --- /dev/null +++ b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll @@ -0,0 +1,20 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 +; RUN: opt -passes=instcombine -S < %s | FileCheck %s + +target datalayout = "E-p:256:256-i256:256:256-S32-a:256:256" + +@G16 = internal constant [10 x i256] [i256 35, i256 82, i256 69, i256 81, i256 85, + i256 73, i256 82, i256 69, i256 68, i256 0] + +define i1 @test_noinbounds(i256 %X) { +; CHECK-LABEL: define i1 @test_noinbounds +; CHECK-SAME: (i256 [[X:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = and i256 [[X]], 576460752303423487 +; CHECK-NEXT: [[R:%.*]] = icmp eq i256 [[TMP1]], 9 +; CHECK-NEXT: ret i1 [[R]] +; + %P = getelementptr [10 x i256], ptr @G16, i256 0, i256 %X + %Q = load i256, ptr %P + %R = icmp eq i256 %Q, 0 + ret i1 %R +} From 95a8b42aa27062ae899281e59d733271e599132c Mon Sep 17 00:00:00 2001 From: Vladimir Radosavljevic Date: Tue, 18 Jun 2024 15:52:39 +0200 Subject: [PATCH 2/2] [InstCombine] Fix miscompile on GEP+load to icmp fold When constructing mask, use -1 that is represented in 256 bits instead of (uint64_t)-1 value. Signed-off-by: Vladimir Radosavljevic --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 4 +++- llvm/test/Transforms/InstCombine/load-cmp-bug.ll | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index ced281d182d3..69f37be2dfbe 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -305,7 +305,9 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( DL.getTypeAllocSize(Init->getType()->getArrayElementType()); auto MaskIdx = [&](Value *Idx) { if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) { - Value *Mask = ConstantInt::get(Idx->getType(), -1); + // EraVM local begin + Value *Mask = ConstantInt::get(Idx->getType(), -1, true); + // EraVM local end Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize)); Idx = Builder.CreateAnd(Idx, Mask); } diff --git a/llvm/test/Transforms/InstCombine/load-cmp-bug.ll b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll index f4605fa5073e..3c481a1f3056 100644 --- a/llvm/test/Transforms/InstCombine/load-cmp-bug.ll +++ b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll @@ -9,7 +9,7 @@ target datalayout = "E-p:256:256-i256:256:256-S32-a:256:256" define i1 @test_noinbounds(i256 %X) { ; CHECK-LABEL: define i1 @test_noinbounds ; CHECK-SAME: (i256 [[X:%.*]]) { -; CHECK-NEXT: [[TMP1:%.*]] = and i256 [[X]], 576460752303423487 +; CHECK-NEXT: [[TMP1:%.*]] = and i256 [[X]], 3618502788666131106986593281521497120414687020801267626233049500247285301247 ; CHECK-NEXT: [[R:%.*]] = icmp eq i256 [[TMP1]], 9 ; CHECK-NEXT: ret i1 [[R]] ;