From ff8fbf1b4b970108a7a79200c2212dc0fde30ab3 Mon Sep 17 00:00:00 2001 From: Anthony Latsis Date: Tue, 13 May 2025 01:05:21 +0100 Subject: [PATCH 1/2] Revert "SILGen, SILOptimizer: Fix assertion failures in `llvm::APInt` ctor" The approach taken in the reverted commit is causing tests to fail, and I am not positive that all instances of the -1 to 1 switch are correct. A subsequent commit will restore the old behavior by tweaking the ctor call instead. This reverts commit 657f5028fb45991569d6f58770e3edb0196ec500. --- lib/SILGen/SILGenDecl.cpp | 9 +++++---- lib/SILOptimizer/LoopTransforms/BoundsCheckOpts.cpp | 6 +++--- lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp | 2 +- lib/SILOptimizer/Transforms/SimplifyCFG.cpp | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/SILGen/SILGenDecl.cpp b/lib/SILGen/SILGenDecl.cpp index ad708d95c2491..cde0970462de2 100644 --- a/lib/SILGen/SILGenDecl.cpp +++ b/lib/SILGen/SILGenDecl.cpp @@ -1995,11 +1995,12 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest, emitOSVersionRangeCheck(loc, versionRange.value(), isMacCatalyst); if (availability->isUnavailability()) { // If this is an unavailability check, invert the result - // by emitting a call to Builtin.xor_Int1(lhs, 1). + // by emitting a call to Builtin.xor_Int1(lhs, -1). SILType i1 = SILType::getBuiltinIntegerType(1, getASTContext()); - SILValue one = B.createIntegerLiteral(loc, i1, 1); - booleanTestValue = B.createBuiltinBinaryFunction( - loc, "xor", i1, i1, {booleanTestValue, one}); + SILValue minusOne = B.createIntegerLiteral(loc, i1, -1); + booleanTestValue = + B.createBuiltinBinaryFunction(loc, "xor", i1, i1, + {booleanTestValue, minusOne}); } } break; diff --git a/lib/SILOptimizer/LoopTransforms/BoundsCheckOpts.cpp b/lib/SILOptimizer/LoopTransforms/BoundsCheckOpts.cpp index 46e8c289f18b0..0e6802be83382 100644 --- a/lib/SILOptimizer/LoopTransforms/BoundsCheckOpts.cpp +++ b/lib/SILOptimizer/LoopTransforms/BoundsCheckOpts.cpp @@ -558,7 +558,7 @@ static SILValue getSub(SILLocation Loc, SILValue Val, unsigned SubVal, SmallVector Args(1, Val); Args.push_back(B.createIntegerLiteral(Loc, Val->getType(), SubVal)); Args.push_back(B.createIntegerLiteral( - Loc, SILType::getBuiltinIntegerType(1, B.getASTContext()), 1)); + Loc, SILType::getBuiltinIntegerType(1, B.getASTContext()), -1)); auto *AI = B.createBuiltinBinaryFunctionWithOverflow( Loc, "ssub_with_overflow", Args); @@ -570,7 +570,7 @@ static SILValue getAdd(SILLocation Loc, SILValue Val, unsigned AddVal, SmallVector Args(1, Val); Args.push_back(B.createIntegerLiteral(Loc, Val->getType(), AddVal)); Args.push_back(B.createIntegerLiteral( - Loc, SILType::getBuiltinIntegerType(1, B.getASTContext()), 1)); + Loc, SILType::getBuiltinIntegerType(1, B.getASTContext()), -1)); auto *AI = B.createBuiltinBinaryFunctionWithOverflow( Loc, "sadd_with_overflow", Args); @@ -1342,7 +1342,7 @@ BoundsCheckOpts::findAndOptimizeInductionVariables(SILLoop *loop) { if (isComparisonKnownTrue(builtin, *ivar)) { if (!trueVal) trueVal = builder.createIntegerLiteral(builtin->getLoc(), - builtin->getType(), 1); + builtin->getType(), -1); builtin->replaceAllUsesWith(trueVal); changed = true; continue; diff --git a/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp b/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp index 65cfe2b47537d..a385a2475d442 100644 --- a/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp +++ b/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp @@ -2976,7 +2976,7 @@ static SILValue testAllControlVariableBits(SILLocation Loc, if (IVType->getFixedWidth() == 1) return CondVal; - SILValue AllBitsSet = B.createIntegerLiteral(Loc, CondVal->getType(), 1); + SILValue AllBitsSet = B.createIntegerLiteral(Loc, CondVal->getType(), -1); if (!CmpEqFn.get()) CmpEqFn = getBinaryFunction("cmp_eq", CondVal->getType(), B.getASTContext()); diff --git a/lib/SILOptimizer/Transforms/SimplifyCFG.cpp b/lib/SILOptimizer/Transforms/SimplifyCFG.cpp index 0ce619a37ef84..95a835cb07a14 100644 --- a/lib/SILOptimizer/Transforms/SimplifyCFG.cpp +++ b/lib/SILOptimizer/Transforms/SimplifyCFG.cpp @@ -304,7 +304,7 @@ static SILValue createValueForEdge(SILInstruction *UserInst, if (auto *CBI = dyn_cast(DominatingTerminator)) return Builder.createIntegerLiteral( - CBI->getLoc(), CBI->getCondition()->getType(), EdgeIdx == 0 ? 1 : 0); + CBI->getLoc(), CBI->getCondition()->getType(), EdgeIdx == 0 ? -1 : 0); auto *SEI = cast(DominatingTerminator); auto *DstBlock = SEI->getSuccessors()[EdgeIdx].getBB(); @@ -1480,7 +1480,7 @@ static SILValue invertExpectAndApplyTo(SILBuilder &Builder, if (!IL) return V; SILValue NegatedExpectedValue = Builder.createIntegerLiteral( - IL->getLoc(), Args[1]->getType(), IL->getValue() == 0 ? 1 : 0); + IL->getLoc(), Args[1]->getType(), IL->getValue() == 0 ? -1 : 0); return Builder.createBuiltin(BI->getLoc(), BI->getName(), BI->getType(), {}, {V, NegatedExpectedValue}); } From 73c70ee338c515fba37baf3faff93c6389558a5c Mon Sep 17 00:00:00 2001 From: Anthony Latsis Date: Tue, 13 May 2025 01:19:48 +0100 Subject: [PATCH 2/2] [rebranch] SIL: Restore old behavior in `llvm::APInt` ctor call See https://github.com/llvm/llvm-project/pull/114539. --- lib/SIL/IR/SILInstructions.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/SIL/IR/SILInstructions.cpp b/lib/SIL/IR/SILInstructions.cpp index 5d4fdee039af2..812397d0f20bb 100644 --- a/lib/SIL/IR/SILInstructions.cpp +++ b/lib/SIL/IR/SILInstructions.cpp @@ -1145,7 +1145,9 @@ IntegerLiteralInst *IntegerLiteralInst::create(SILDebugLocation Loc, static APInt getAPInt(AnyBuiltinIntegerType *anyIntTy, intmax_t value) { // If we're forming a fixed-width type, build using the greatest width. if (auto intTy = dyn_cast(anyIntTy)) - return APInt(intTy->getGreatestWidth(), value); + // TODO: Avoid implicit trunc? + return APInt(intTy->getGreatestWidth(), value, /*isSigned=*/false, + /*implicitTrunc=*/true); // Otherwise, build using the size of the type and then truncate to the // minimum width necessary.