Skip to content

[CIR] Always zero-extend shift amounts #1568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2967,10 +2967,11 @@ mlir::LogicalResult CIRToLLVMShiftOpLowering::matchAndRewrite(
// behavior might occur in the casts below as per [C99 6.5.7.3].
// Vector type shift amount needs no cast as type consistency is expected to
// be already be enforced at CIRGen.
// Negative shift amounts are undefined behavior so we can always zero extend
// the integer here.
if (cirAmtTy)
amt = getLLVMIntCast(rewriter, amt, mlir::cast<mlir::IntegerType>(llvmTy),
!cirAmtTy.isSigned(), cirAmtTy.getWidth(),
cirValTy.getWidth());
true, cirAmtTy.getWidth(), cirValTy.getWidth());

// Lower to the proper LLVM shift operation.
if (op.getIsShiftleft())
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/shift.cir
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module {

// Should allow shift with signed smaller amount type.
%2 = cir.shift(left, %arg1 : !s32i, %arg0 : !s16i) -> !s32i
// CHECK: %[[#CAST:]] = llvm.sext %{{.+}} : i16 to i32
// CHECK: %[[#CAST:]] = llvm.zext %{{.+}} : i16 to i32
// CHECK: llvm.shl %{{.+}}, %[[#CAST]] : i32

// Should allow shift with unsigned smaller amount type.
Expand Down
Loading