https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/234472
>From eedf622840d126641b86804d57ebd3571a17254a Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Thu, 22 Oct 2026 10:34:13 +0800 Subject: [PATCH 1/4] Precommit tests --- .../ConstProp/mask-beforefirst.ll | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll new file mode 100644 index 00000000000000..dd88f618a0b356 --- /dev/null +++ b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll @@ -0,0 +1,74 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 +; RUN: opt -p instsimplify -S < %s | FileCheck %s + +define <4 x i1> @v4i1_zero() { +; CHECK-LABEL: define <4 x i1> @v4i1_zero() { +; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> zeroinitializer) +; CHECK-NEXT: ret <4 x i1> [[RES]] +; + %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> zeroinitializer) + ret <4 x i1> %res +} + +define <4 x i1> @v4i1_ones() { +; CHECK-LABEL: define <4 x i1> @v4i1_ones() { +; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> splat (i1 true)) +; CHECK-NEXT: ret <4 x i1> [[RES]] +; + %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> splat (i1 true)) + ret <4 x i1> %res +} + +define <4 x i1> @v4i1() { +; CHECK-LABEL: define <4 x i1> @v4i1() { +; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 false, i1 true, i1 false, i1 true>) +; CHECK-NEXT: ret <4 x i1> [[RES]] +; + %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 false, i1 true, i1 false, i1 true>) + ret <4 x i1> %res +} + +define <4 x i1> @v4i1_undef() { +; CHECK-LABEL: define <4 x i1> @v4i1_undef() { +; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 undef, i1 true, i1 false, i1 false>) +; CHECK-NEXT: ret <4 x i1> [[RES]] +; + %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 undef, i1 true, i1 false, i1 false>) + ret <4 x i1> %res +} + +define <4 x i1> @v4i1_poison() { +; CHECK-LABEL: define <4 x i1> @v4i1_poison() { +; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 false, i1 true, i1 poison, i1 false>) +; CHECK-NEXT: ret <4 x i1> [[RES]] +; + %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 false, i1 true, i1 poison, i1 false>) + ret <4 x i1> %res +} + +define <vscale x 4 x i1> @nxv4i1_zero() { +; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_zero() { +; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> zeroinitializer) +; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; + %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> zeroinitializer) + ret <vscale x 4 x i1> %res +} + +define <vscale x 4 x i1> @nxv4i1_ones() { +; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_ones() { +; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> splat (i1 true)) +; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; + %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> splat (i1 true)) + ret <vscale x 4 x i1> %res +} + +define <vscale x 4 x i1> @nxv4i1_poison() { +; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_poison() { +; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> poison) +; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; + %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> poison) + ret <vscale x 4 x i1> %res +} >From 4fea4b46abeb27c368c3116e65f3da46199199a7 Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Thu, 22 Oct 2026 10:35:48 +0800 Subject: [PATCH 2/4] [ConstantFolding] Add constant folding for mask.beforefirst Poison lanes poison the entire result, and undef elements are ignored Assisted-by: Opus 5.5 --- llvm/lib/Analysis/ConstantFolding.cpp | 23 ++++++++++++++++++ .../ConstProp/mask-beforefirst.ll | 24 +++++++------------ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 5169450936dc36..3219248a99e5dd 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1837,6 +1837,7 @@ static bool canConstantFoldIntrinsic(Intrinsic::ID ID, bool IsStrictFP) { case Intrinsic::launder_invariant_group: case Intrinsic::masked_load: case Intrinsic::get_active_lane_mask: + case Intrinsic::mask_beforefirst: case Intrinsic::abs: case Intrinsic::smax: case Intrinsic::smin: @@ -4551,6 +4552,20 @@ static Constant *ConstantFoldFixedVectorCall( } return nullptr; } + case Intrinsic::mask_beforefirst: { + bool SeenActive = false; + for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) { + Constant *Elt = Operands[0]->getAggregateElement(I); + if (isa_and_nonnull<PoisonValue>(Elt)) + return PoisonValue::get(FVTy); + if (auto *CI = dyn_cast_or_null<ConstantInt>(Elt)) + SeenActive |= CI->isOne(); + else if (!isa_and_nonnull<UndefValue>(Elt)) + return nullptr; + Result[I] = ConstantInt::getBool(Ty, !SeenActive); + } + return ConstantVector::get(Result); + } case Intrinsic::vector_extract: { auto *Idx = dyn_cast<ConstantInt>(Operands[1]); Constant *Vec = Operands[0]; @@ -4711,6 +4726,14 @@ static Constant *ConstantFoldScalableVectorCall( return ConstantVector::getNullValue(SVTy); break; } + case Intrinsic::mask_beforefirst: { + Constant *SplatVal = Operands[0]->getSplatValue(); + if (isa_and_nonnull<PoisonValue>(SplatVal)) + return PoisonValue::get(SVTy); + if (auto *CI = dyn_cast_if_present<ConstantInt>(SplatVal)) + return ConstantInt::getBool(SVTy, CI->isZero()); + break; + } case Intrinsic::vector_interleave2: case Intrinsic::vector_interleave3: case Intrinsic::vector_interleave4: diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll index dd88f618a0b356..24c8fbdcbff680 100644 --- a/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll +++ b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll @@ -3,8 +3,7 @@ define <4 x i1> @v4i1_zero() { ; CHECK-LABEL: define <4 x i1> @v4i1_zero() { -; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> zeroinitializer) -; CHECK-NEXT: ret <4 x i1> [[RES]] +; CHECK-NEXT: ret <4 x i1> splat (i1 true) ; %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> zeroinitializer) ret <4 x i1> %res @@ -12,8 +11,7 @@ define <4 x i1> @v4i1_zero() { define <4 x i1> @v4i1_ones() { ; CHECK-LABEL: define <4 x i1> @v4i1_ones() { -; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> splat (i1 true)) -; CHECK-NEXT: ret <4 x i1> [[RES]] +; CHECK-NEXT: ret <4 x i1> zeroinitializer ; %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> splat (i1 true)) ret <4 x i1> %res @@ -21,8 +19,7 @@ define <4 x i1> @v4i1_ones() { define <4 x i1> @v4i1() { ; CHECK-LABEL: define <4 x i1> @v4i1() { -; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 false, i1 true, i1 false, i1 true>) -; CHECK-NEXT: ret <4 x i1> [[RES]] +; CHECK-NEXT: ret <4 x i1> <i1 true, i1 false, i1 false, i1 false> ; %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 false, i1 true, i1 false, i1 true>) ret <4 x i1> %res @@ -30,8 +27,7 @@ define <4 x i1> @v4i1() { define <4 x i1> @v4i1_undef() { ; CHECK-LABEL: define <4 x i1> @v4i1_undef() { -; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 undef, i1 true, i1 false, i1 false>) -; CHECK-NEXT: ret <4 x i1> [[RES]] +; CHECK-NEXT: ret <4 x i1> <i1 true, i1 false, i1 false, i1 false> ; %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 undef, i1 true, i1 false, i1 false>) ret <4 x i1> %res @@ -39,8 +35,7 @@ define <4 x i1> @v4i1_undef() { define <4 x i1> @v4i1_poison() { ; CHECK-LABEL: define <4 x i1> @v4i1_poison() { -; CHECK-NEXT: [[RES:%.*]] = call <4 x i1> @llvm.mask.beforefirst.v4i1(<4 x i1> <i1 false, i1 true, i1 poison, i1 false>) -; CHECK-NEXT: ret <4 x i1> [[RES]] +; CHECK-NEXT: ret <4 x i1> poison ; %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 false, i1 true, i1 poison, i1 false>) ret <4 x i1> %res @@ -48,8 +43,7 @@ define <4 x i1> @v4i1_poison() { define <vscale x 4 x i1> @nxv4i1_zero() { ; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_zero() { -; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> zeroinitializer) -; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; CHECK-NEXT: ret <vscale x 4 x i1> splat (i1 true) ; %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> zeroinitializer) ret <vscale x 4 x i1> %res @@ -57,8 +51,7 @@ define <vscale x 4 x i1> @nxv4i1_zero() { define <vscale x 4 x i1> @nxv4i1_ones() { ; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_ones() { -; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> splat (i1 true)) -; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; CHECK-NEXT: ret <vscale x 4 x i1> zeroinitializer ; %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> splat (i1 true)) ret <vscale x 4 x i1> %res @@ -66,8 +59,7 @@ define <vscale x 4 x i1> @nxv4i1_ones() { define <vscale x 4 x i1> @nxv4i1_poison() { ; CHECK-LABEL: define <vscale x 4 x i1> @nxv4i1_poison() { -; CHECK-NEXT: [[RES:%.*]] = call <vscale x 4 x i1> @llvm.mask.beforefirst.nxv4i1(<vscale x 4 x i1> poison) -; CHECK-NEXT: ret <vscale x 4 x i1> [[RES]] +; CHECK-NEXT: ret <vscale x 4 x i1> poison ; %res = call <vscale x 4 x i1> @llvm.mask.beforefirst(<vscale x 4 x i1> poison) ret <vscale x 4 x i1> %res >From 697541322856dd3e165f0a4f6a5c97fcb2852678 Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Thu, 22 Oct 2026 16:27:56 +0800 Subject: [PATCH 3/4] Remove undef handling --- llvm/lib/Analysis/ConstantFolding.cpp | 6 +++--- .../Transforms/InstSimplify/ConstProp/mask-beforefirst.ll | 8 -------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 3219248a99e5dd..a0c5357f72a46d 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -4558,10 +4558,10 @@ static Constant *ConstantFoldFixedVectorCall( Constant *Elt = Operands[0]->getAggregateElement(I); if (isa_and_nonnull<PoisonValue>(Elt)) return PoisonValue::get(FVTy); - if (auto *CI = dyn_cast_or_null<ConstantInt>(Elt)) - SeenActive |= CI->isOne(); - else if (!isa_and_nonnull<UndefValue>(Elt)) + auto *CI = dyn_cast_or_null<ConstantInt>(Elt); + if (!CI) return nullptr; + SeenActive |= CI->isOne(); Result[I] = ConstantInt::getBool(Ty, !SeenActive); } return ConstantVector::get(Result); diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll index 24c8fbdcbff680..8033be0bc02d98 100644 --- a/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll +++ b/llvm/test/Transforms/InstSimplify/ConstProp/mask-beforefirst.ll @@ -25,14 +25,6 @@ define <4 x i1> @v4i1() { ret <4 x i1> %res } -define <4 x i1> @v4i1_undef() { -; CHECK-LABEL: define <4 x i1> @v4i1_undef() { -; CHECK-NEXT: ret <4 x i1> <i1 true, i1 false, i1 false, i1 false> -; - %res = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> <i1 undef, i1 true, i1 false, i1 false>) - ret <4 x i1> %res -} - define <4 x i1> @v4i1_poison() { ; CHECK-LABEL: define <4 x i1> @v4i1_poison() { ; CHECK-NEXT: ret <4 x i1> poison >From eb95af82f5329a11ec2d5206a6631cc776959496 Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Thu, 22 Oct 2026 23:30:57 +0800 Subject: [PATCH 4/4] Address review comments --- llvm/lib/Analysis/ConstantFolding.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index a0c5357f72a46d..069809a16545ef 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -4553,7 +4553,9 @@ static Constant *ConstantFoldFixedVectorCall( return nullptr; } case Intrinsic::mask_beforefirst: { - bool SeenActive = false; + Constant *True = ConstantInt::getTrue(Ty); + Constant *False = ConstantInt::getFalse(Ty); + Constant *Current = True; for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) { Constant *Elt = Operands[0]->getAggregateElement(I); if (isa_and_nonnull<PoisonValue>(Elt)) @@ -4561,8 +4563,9 @@ static Constant *ConstantFoldFixedVectorCall( auto *CI = dyn_cast_or_null<ConstantInt>(Elt); if (!CI) return nullptr; - SeenActive |= CI->isOne(); - Result[I] = ConstantInt::getBool(Ty, !SeenActive); + if (CI->isOne()) + Current = False; + Result[I] = Current; } return ConstantVector::get(Result); } _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
