From 466b58ba3809efa4c82e950a62065b58b8c696bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Clement=20=28=E3=83=90=E3=83=AC=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=B3=20=E3=82=AF=E3=83=AC=E3=83=A1?= =?UTF-8?q?=E3=83=B3=29?= Date: Thu, 31 Oct 2024 18:59:04 -0700 Subject: [PATCH] [flang] Avoid generating duplicate symbol in comdat (#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. --- flang/lib/Optimizer/CodeGen/CodeGen.cpp | 3 +++ flang/test/Fir/comdat-present.fir | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 flang/test/Fir/comdat-present.fir diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 4c8c56e0f21cef..d038efcb2eb42c 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -2931,6 +2931,9 @@ struct GlobalOpConversion : public fir::FIROpConversion { comdatOp = rewriter.create(module.getLoc(), comdatName); } + if (auto select = comdatOp.lookupSymbol( + global.getSymName())) + return; mlir::OpBuilder::InsertionGuard guard(rewriter); rewriter.setInsertionPointToEnd(&comdatOp.getBody().back()); auto selectorOp = rewriter.create( diff --git a/flang/test/Fir/comdat-present.fir b/flang/test/Fir/comdat-present.fir new file mode 100644 index 00000000000000..96d14e5973f4f7 --- /dev/null +++ b/flang/test/Fir/comdat-present.fir @@ -0,0 +1,14 @@ +// RUN: fir-opt %s --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %s +// RUN: fir-opt %s --fir-to-llvm-ir="target=x86_64-pc-windows-msvc" | FileCheck %s + +fir.global linkonce_odr @global_linkonce_odr constant : i32 { + %0 = arith.constant 0 : i32 + fir.has_value %0 : i32 +} + +llvm.comdat @__llvm_comdat { + llvm.comdat_selector @global_linkonce_odr any +} + +// CHECK-LABEL: llvm.comdat @__llvm_comdat +// CHECK: llvm.comdat_selector @global_linkonce_odr any