Skip to content

Commit c9a89cd

Browse files
andykaylorHoney Goyal
authored andcommitted
[MLIR] Zero-extend unsigned and 1-bit values when translating IntegerAttr (llvm#169751)
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 11ff610 commit c9a89cd

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
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

flang/test/Fir/global.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fir.global internal @_QEmultiarray : !fir.array<32x32xi32> {
5050
fir.has_value %2 : !fir.array<32x32xi32>
5151
}
5252

53-
// CHECK: @_QEmasklogical = internal global [32768 x i32] [i32 -1, i32 -1,
53+
// CHECK: @_QEmasklogical = internal global [32768 x i32] [i32 1, i32 1,
5454
fir.global internal @_QEmasklogical : !fir.array<32768x!fir.logical<4>> {
5555
%true = arith.constant true
5656
%0 = fir.undefined !fir.array<32768x!fir.logical<4>>

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 = 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)