Skip to content

Commit c2ac219

Browse files
nikicjoaosaffran
authored and
joaosaffran
committed
[MLIR][LLVMIR] Use TargetFolder when creating globals (llvm#126745)
The LLVM dialect lowers globals using IRBuilder, relying on it creating constant expressions where possible. As we remove support for more constant expressions (per https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179), this can cause issues for cases where the constant expression is no longer supported, and the operation cannot be constant folded without DataLayout being available. In particular, I ran into this issue with flang and the removal of mul constant expressions. Address this by using TargetFolder when creating globals, which will perform DL-aware constant folding. I think it would make sense to also do this in general, but I'm starting with globals where not doing this can result in translation failures. Ideally, globals with these problematic expressions would never be generated in the first place, but there has been little movement on fixing this (llvm#96047).
1 parent 03ce7ed commit c2ac219

File tree

6 files changed

+14
-9
lines changed

6 files changed

+14
-9
lines changed

flang/test/Fir/box.fir

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// RUN: tco -o - %s | FileCheck %s
22

33
// Global box initialization (test must come first because llvm globals are emitted first).
4-
// CHECK-LABEL: @globalx = internal global { ptr, i64, i32, i8, i8, i8, i8 } { ptr null, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i32 20240719, i8 0, i8 9, i8 2, i8 0 }
4+
// CHECK-LABEL: @globalx = internal global { ptr, i64, i32, i8, i8, i8, i8 } { ptr null, i64 4, i32 20240719, i8 0, i8 9, i8 2, i8 0 }
55
fir.global internal @globalx : !fir.box<!fir.heap<i32>> {
66
%c0 = arith.constant 0 : index
77
%0 = fir.convert %c0 : (index) -> !fir.heap<i32>
88
%1 = fir.embox %0 : (!fir.heap<i32>) -> !fir.box<!fir.heap<i32>>
99
fir.has_value %1 : !fir.box<!fir.heap<i32>>
1010
}
1111

12-
// CHECK-LABEL: @globaly = internal global { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr null, i64 ptrtoint (ptr getelementptr (float, ptr null, i32 1) to i64), i32 20240719, i8 1, i8 27, i8 2, i8 0,{{.*}}[3 x i64] [i64 1, i64 0, i64 ptrtoint (ptr getelementptr (float, ptr null, i32 1) to i64)]
12+
// CHECK-LABEL: @globaly = internal global { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr null, i64 4, i32 20240719, i8 1, i8 27, i8 2, i8 0,{{.*}}[3 x i64] [i64 1, i64 0, i64 4]
1313
fir.global internal @globaly : !fir.box<!fir.heap<!fir.array<?xf32>>> {
1414
%c0 = arith.constant 0 : index
1515
%0 = fir.convert %c0 : (index) -> !fir.heap<!fir.array<?xf32>>

flang/test/Fir/rebox-global.fir

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ fir.global @pointer_char4_init : !fir.box<!fir.ptr<!fir.char<4,10>>> {
2020
fir.has_value %2 : !fir.box<!fir.ptr<!fir.char<4,10>>>
2121
}
2222
// CHECK-LABEL: @pointer_char4_init
23-
// CHECK-SAME: { ptr @char4, i64 ptrtoint (ptr getelementptr ([10 x i32], ptr null, i32 1) to i64), i32 20240719, i8 0, i8 44, i8 1, i8 0 }
23+
// CHECK-SAME: { ptr @char4, i64 40, i32 20240719, i8 0, i8 44, i8 1, i8 0 }

flang/test/Fir/type-descriptor.fir

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fir.global internal @_QFfooEx : !fir.box<!fir.heap<!sometype>> {
1313
fir.has_value %1 : !fir.box<!fir.heap<!sometype>>
1414
}
1515
// CHECK: @_QFfooEx = internal global { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }
16-
// CHECK-SAME: { ptr null, i64 ptrtoint (ptr getelementptr (%_QFfooTsometype, ptr null, i32 1) to i64),
16+
// CHECK-SAME: { ptr null, i64 80,
1717
// CHECK-SAME: i32 20240719, i8 0, i8 42, i8 2, i8 1, ptr @_QFfooEXdtXsometype, [1 x i64] zeroinitializer }
1818

1919
!some_pdt_type = !fir.type<_QFfooTsome_pdt_typeK42K43{num:i32,values:!fir.box<!fir.ptr<!fir.array<?x?xf32>>>}>
@@ -25,5 +25,5 @@ fir.global internal @_QFfooEx2 : !fir.box<!fir.heap<!some_pdt_type>> {
2525
fir.has_value %1 : !fir.box<!fir.heap<!some_pdt_type>>
2626
}
2727
// CHECK: @_QFfooEx2 = internal global { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }
28-
// CHECK-SAME: { ptr null, i64 ptrtoint (ptr getelementptr (%_QFfooTsome_pdt_typeK42K43, ptr null, i32 1) to i64),
28+
// CHECK-SAME: { ptr null, i64 80,
2929
// CHECK-SAME: i32 20240719, i8 0, i8 42, i8 2, i8 1, ptr @_QFfooEXdtXsome_pdt_typeX42X43, [1 x i64] zeroinitializer }

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/ADT/SetVector.h"
3939
#include "llvm/ADT/StringExtras.h"
4040
#include "llvm/ADT/TypeSwitch.h"
41+
#include "llvm/Analysis/TargetFolder.h"
4142
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
4243
#include "llvm/IR/BasicBlock.h"
4344
#include "llvm/IR/CFG.h"
@@ -1171,7 +1172,9 @@ LogicalResult ModuleTranslation::convertGlobalsAndAliases() {
11711172
// Convert global variable bodies.
11721173
for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
11731174
if (Block *initializer = op.getInitializerBlock()) {
1174-
llvm::IRBuilder<> builder(llvmModule->getContext());
1175+
llvm::IRBuilder<llvm::TargetFolder> builder(
1176+
llvmModule->getContext(),
1177+
llvm::TargetFolder(llvmModule->getDataLayout()));
11751178

11761179
[[maybe_unused]] int numConstantsHit = 0;
11771180
[[maybe_unused]] int numConstantsErased = 0;
@@ -1282,7 +1285,9 @@ LogicalResult ModuleTranslation::convertGlobalsAndAliases() {
12821285
// Convert global alias bodies.
12831286
for (auto op : getModuleBody(mlirModule).getOps<LLVM::AliasOp>()) {
12841287
Block &initializer = op.getInitializerBlock();
1285-
llvm::IRBuilder<> builder(llvmModule->getContext());
1288+
llvm::IRBuilder<llvm::TargetFolder> builder(
1289+
llvmModule->getContext(),
1290+
llvm::TargetFolder(llvmModule->getDataLayout()));
12861291

12871292
for (mlir::Operation &op : initializer.without_terminator()) {
12881293
if (failed(convertOperation(op, builder)))

mlir/test/Target/LLVMIR/llvmir.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ llvm.mlir.global external @explicit_undef() : i32 {
8484
llvm.return %0 : i32
8585
}
8686

87-
// CHECK: @int_gep = internal constant ptr getelementptr (i32, ptr @i32_global, i32 2)
87+
// CHECK: @int_gep = internal constant ptr getelementptr (i8, ptr @i32_global, i64 8)
8888
llvm.mlir.global internal constant @int_gep() : !llvm.ptr {
8989
%addr = llvm.mlir.addressof @i32_global : !llvm.ptr
9090
%_c0 = llvm.mlir.constant(2: i32) : i32

mlir/test/Target/LLVMIR/omptarget-declare-target-llvm-host.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ module attributes {llvm.target_triple = "x86_64-unknown-linux-gnu", omp.is_targe
133133
llvm.return %0 : i32
134134
}
135135

136-
// CHECK-DAG: @_QMtest_0Ept1 = global { ptr, i64, i32, i8, i8, i8, i8 } { ptr null, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i32 20180515, i8 0, i8 9, i8 1, i8 0 }
136+
// CHECK-DAG: @_QMtest_0Ept1 = global { ptr, i64, i32, i8, i8, i8, i8 } { ptr null, i64 4, i32 20180515, i8 0, i8 9, i8 1, i8 0 }
137137
// CHECK-DAG: @_QMtest_0Ept1_decl_tgt_ref_ptr = weak global ptr @_QMtest_0Ept1
138138
// CHECK-DAG: @.offloading.entry_name{{.*}} = internal unnamed_addr constant [31 x i8] c"_QMtest_0Ept1_decl_tgt_ref_ptr\00"
139139
// CHECK-DAG: @.offloading.entry._QMtest_0Ept1_decl_tgt_ref_ptr = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 1, i32 1, ptr @_QMtest_0Ept1_decl_tgt_ref_ptr, ptr @.offloading.entry_name{{.*}}, i64 8, i64 0, ptr null }, section "llvm_offload_entries"

0 commit comments

Comments
 (0)