Skip to content

Commit 4a4568f

Browse files
committed
[MLIR] Zero-extend unsigned and 1-bit integers when translating to LLVM IR
This updates the LLVM IR ConstantInt creation from mlir::IntegerAttr so that unsigned integers and 1-bit integers are zero-extended rather than sign-extended.
1 parent e52aece commit 4a4568f

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

clang/test/CIR/CodeGen/globals.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ int *constArrAddr = &arr[2][1];
3535
// LLVM: @constArrAddr = global ptr getelementptr inbounds nuw (i8, ptr @arr, i64 132), align 8
3636

3737
// OGCG: @constArrAddr = global ptr getelementptr (i8, ptr @arr, i64 132), align 8
38+
39+
bool bool_global = true;
40+
41+
// CIR: cir.global external @bool_global = #true {alignment = 1 : i64}
42+
// LLVM: @bool_global = global i8 1, align 1
43+
// OGCG: @bool_global = global i8 1, align 1

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,17 @@ llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
588588
}
589589
// For integer types, we allow a mismatch in sizes as the index type in
590590
// MLIR might have a different size than the index type in the LLVM module.
591-
if (auto intAttr = dyn_cast<IntegerAttr>(attr))
592-
return llvm::ConstantInt::get(
593-
llvmType,
594-
intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
591+
if (auto intAttr = dyn_cast<IntegerAttr>(attr)) {
592+
// If the attribute is an unsigned integer or a 1-bit integer, zero-extend
593+
// the value to the bit width of the LLVM type. Otherwise, sign-extend.
594+
auto intTy = mlir::dyn_cast<IntegerType>(intAttr.getType());
595+
APInt value;
596+
if (intTy && (intTy.isUnsigned() || intTy.getWidth() == 1))
597+
value = intAttr.getValue().zextOrTrunc(llvmType->getIntegerBitWidth());
598+
else
599+
value = intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth());
600+
return llvm::ConstantInt::get(llvmType, value);
601+
}
595602
if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
596603
const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
597604
// Special case for 8-bit floats, which are represented by integers due to

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ llvm.mlir.global internal @f8E8M0FNU_global_as_i8(1.0 : f8E8M0FNU) : i8
7878
// CHECK: @bf16_global_as_i16 = internal global i16 16320
7979
llvm.mlir.global internal @bf16_global_as_i16(1.5 : bf16) : i16
8080

81+
// CHECK: @bool_global_as_i8 = internal global i8 1
82+
llvm.mlir.global internal @bool_global_as_i8(true) : i8
83+
8184
// CHECK: @explicit_undef = global i32 undef
8285
llvm.mlir.global external @explicit_undef() : i32 {
8386
%0 = llvm.mlir.undef : i32

0 commit comments

Comments
 (0)