Skip to content

Commit 466b58b

Browse files
authored
[flang] Avoid generating duplicate symbol in comdat (llvm#114472)
In case where a fir.global might be duplicated in an inner module (gpu.module), the conversion pattern will be applied on the module and the gpu module version of the global and try to generate multiple comdat with the same symbol name. This is what we have in the implementation of CUDA Fortran. Just check for the presence of the `ComdatSelectorOp` before creating a new one.
1 parent 067ce5c commit 466b58b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,6 +2931,9 @@ struct GlobalOpConversion : public fir::FIROpConversion<fir::GlobalOp> {
29312931
comdatOp =
29322932
rewriter.create<mlir::LLVM::ComdatOp>(module.getLoc(), comdatName);
29332933
}
2934+
if (auto select = comdatOp.lookupSymbol<mlir::LLVM::ComdatSelectorOp>(
2935+
global.getSymName()))
2936+
return;
29342937
mlir::OpBuilder::InsertionGuard guard(rewriter);
29352938
rewriter.setInsertionPointToEnd(&comdatOp.getBody().back());
29362939
auto selectorOp = rewriter.create<mlir::LLVM::ComdatSelectorOp>(

flang/test/Fir/comdat-present.fir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: fir-opt %s --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %s
2+
// RUN: fir-opt %s --fir-to-llvm-ir="target=x86_64-pc-windows-msvc" | FileCheck %s
3+
4+
fir.global linkonce_odr @global_linkonce_odr constant : i32 {
5+
%0 = arith.constant 0 : i32
6+
fir.has_value %0 : i32
7+
}
8+
9+
llvm.comdat @__llvm_comdat {
10+
llvm.comdat_selector @global_linkonce_odr any
11+
}
12+
13+
// CHECK-LABEL: llvm.comdat @__llvm_comdat
14+
// CHECK: llvm.comdat_selector @global_linkonce_odr any

0 commit comments

Comments
 (0)