Skip to content

Commit ce2c8b0

Browse files
committed
Add support for lowering floating-point globals
1 parent 6a2570c commit ce2c8b0

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
7575
SmallVector<mlir::NamedAttribute> attributes;
7676

7777
if (init.has_value()) {
78-
if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(init.value())) {
78+
if (auto fltAttr = mlir::dyn_cast<cir::FPAttr>(init.value())) {
79+
// Initializer is a constant floating-point number: convert to MLIR
80+
// builtin constant.
81+
init = rewriter.getFloatAttr(llvmType, fltAttr.getValue());
82+
} else if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(init.value())) {
7983
// Initializer is a constant array: convert it to a compatible llvm init.
8084
init = rewriter.getIntegerAttr(llvmType, intAttr.getValue());
8185
} else {
@@ -99,6 +103,27 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
99103
// LLVM doesn't work with signed types, so we drop the CIR signs here.
100104
return mlir::IntegerType::get(type.getContext(), type.getWidth());
101105
});
106+
converter.addConversion([&](cir::SingleType type) -> mlir::Type {
107+
return mlir::Float32Type::get(type.getContext());
108+
});
109+
converter.addConversion([&](cir::DoubleType type) -> mlir::Type {
110+
return mlir::Float64Type::get(type.getContext());
111+
});
112+
converter.addConversion([&](cir::FP80Type type) -> mlir::Type {
113+
return mlir::Float80Type::get(type.getContext());
114+
});
115+
converter.addConversion([&](cir::FP128Type type) -> mlir::Type {
116+
return mlir::Float128Type::get(type.getContext());
117+
});
118+
converter.addConversion([&](cir::LongDoubleType type) -> mlir::Type {
119+
return converter.convertType(type.getUnderlying());
120+
});
121+
converter.addConversion([&](cir::FP16Type type) -> mlir::Type {
122+
return mlir::Float16Type::get(type.getContext());
123+
});
124+
converter.addConversion([&](cir::BF16Type type) -> mlir::Type {
125+
return mlir::BFloat16Type::get(type.getContext());
126+
});
102127
}
103128

104129
void ConvertCIRToLLVMPass::runOnOperation() {

clang/test/CIR/Lowering/global-var-simple.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,21 @@ _BitInt(20) sb20;
6161

6262
unsigned _BitInt(48) ub48;
6363
// CHECK: @ub48 = external dso_local global i48
64+
65+
_Float16 f16;
66+
// CHECK: @f16 = external dso_local global half
67+
68+
__bf16 bf16;
69+
// CHECK: @bf16 = external dso_local global bfloat
70+
71+
float f;
72+
// CHECK: @f = external dso_local global float
73+
74+
double d = 1.25;
75+
// CHECK: @d = dso_local global double 1.250000e+00
76+
77+
long double ld;
78+
// CHECK: @ld = external dso_local global x86_fp80
79+
80+
__float128 f128;
81+
// CHECK: @f128 = external dso_local global fp128

0 commit comments

Comments
 (0)