diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 082f2b15512b8..6958356add813 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -1247,10 +1247,10 @@ struct EmboxCommonConversion : public fir::FIROpConversion { /// Get the address of the type descriptor global variable that was created by /// lowering for derived type \p recType. - mlir::Value getTypeDescriptor(mlir::ModuleOp mod, - mlir::ConversionPatternRewriter &rewriter, - mlir::Location loc, - fir::RecordType recType) const { + template + mlir::Value + getTypeDescriptor(ModOpTy mod, mlir::ConversionPatternRewriter &rewriter, + mlir::Location loc, fir::RecordType recType) const { std::string name = this->options.typeDescriptorsRenamedForAssembly ? fir::NameUniquer::getTypeDescriptorAssemblyName(recType.getName()) @@ -1275,7 +1275,8 @@ struct EmboxCommonConversion : public fir::FIROpConversion { return rewriter.create(loc, llvmPtrTy); } - mlir::Value populateDescriptor(mlir::Location loc, mlir::ModuleOp mod, + template + mlir::Value populateDescriptor(mlir::Location loc, ModOpTy mod, fir::BaseBoxType boxTy, mlir::Type inputType, mlir::ConversionPatternRewriter &rewriter, unsigned rank, mlir::Value eleSize, @@ -1414,10 +1415,16 @@ struct EmboxCommonConversion : public fir::FIROpConversion { extraField = this->getExtraFromBox(loc, sourceBoxTyPair, sourceBox, rewriter); } - auto mod = box->template getParentOfType(); - mlir::Value descriptor = - populateDescriptor(loc, mod, boxTy, inputType, rewriter, rank, eleSize, - cfiTy, typeDesc, allocatorIdx, extraField); + + mlir::Value descriptor; + if (auto gpuMod = box->template getParentOfType()) + descriptor = populateDescriptor(loc, gpuMod, boxTy, inputType, rewriter, + rank, eleSize, cfiTy, typeDesc, + allocatorIdx, extraField); + else if (auto mod = box->template getParentOfType()) + descriptor = populateDescriptor(loc, mod, boxTy, inputType, rewriter, + rank, eleSize, cfiTy, typeDesc, + allocatorIdx, extraField); return {boxTy, descriptor, eleSize}; } @@ -1460,11 +1467,17 @@ struct EmboxCommonConversion : public fir::FIROpConversion { mlir::Value extraField = this->getExtraFromBox(loc, inputBoxTyPair, loweredBox, rewriter); - auto mod = box->template getParentOfType(); - mlir::Value descriptor = - populateDescriptor(loc, mod, boxTy, box.getBox().getType(), rewriter, - rank, eleSize, cfiTy, typeDesc, - /*allocatorIdx=*/kDefaultAllocator, extraField); + mlir::Value descriptor; + if (auto gpuMod = box->template getParentOfType()) + descriptor = + populateDescriptor(loc, gpuMod, boxTy, box.getBox().getType(), + rewriter, rank, eleSize, cfiTy, typeDesc, + /*allocatorIdx=*/kDefaultAllocator, extraField); + else if (auto mod = box->template getParentOfType()) + descriptor = + populateDescriptor(loc, mod, boxTy, box.getBox().getType(), rewriter, + rank, eleSize, cfiTy, typeDesc, + /*allocatorIdx=*/kDefaultAllocator, extraField); return {boxTy, descriptor, eleSize}; } diff --git a/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp b/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp index 7f2cc41275e59..f92c60908b149 100644 --- a/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp +++ b/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp @@ -11,6 +11,7 @@ #include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Optimizer/Transforms/Passes.h" +#include "mlir/Dialect/GPU/IR/GPUDialect.h" #include "mlir/IR/Attributes.h" #include "mlir/IR/SymbolTable.h" #include "mlir/Pass/Pass.h" @@ -42,24 +43,31 @@ void CompilerGeneratedNamesConversionPass::runOnOperation() { auto *context = &getContext(); llvm::DenseMap remappings; - for (auto &funcOrGlobal : op->getRegion(0).front()) { - if (llvm::isa(funcOrGlobal) || - llvm::isa(funcOrGlobal)) { - auto symName = funcOrGlobal.getAttrOfType( - mlir::SymbolTable::getSymbolAttrName()); - auto deconstructedName = fir::NameUniquer::deconstruct(symName); - if (deconstructedName.first != fir::NameUniquer::NameKind::NOT_UNIQUED && - !fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) { - std::string newName = - fir::NameUniquer::replaceSpecialSymbols(symName.getValue().str()); - if (newName != symName) { - auto newAttr = mlir::StringAttr::get(context, newName); - mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr); - auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr); - remappings.try_emplace(symName, newSymRef); - } + + auto processOp = [&](mlir::Operation &op) { + auto symName = op.getAttrOfType( + mlir::SymbolTable::getSymbolAttrName()); + auto deconstructedName = fir::NameUniquer::deconstruct(symName); + if (deconstructedName.first != fir::NameUniquer::NameKind::NOT_UNIQUED && + !fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) { + std::string newName = + fir::NameUniquer::replaceSpecialSymbols(symName.getValue().str()); + if (newName != symName) { + auto newAttr = mlir::StringAttr::get(context, newName); + mlir::SymbolTable::setSymbolName(&op, newAttr); + auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr); + remappings.try_emplace(symName, newSymRef); } } + }; + for (auto &op : op->getRegion(0).front()) { + if (llvm::isa(op) || llvm::isa(op)) + processOp(op); + else if (auto gpuMod = mlir::dyn_cast(&op)) + for (auto &op : gpuMod->getRegion(0).front()) + if (llvm::isa(op) || llvm::isa(op) || + llvm::isa(op)) + processOp(op); } if (remappings.empty()) diff --git a/flang/test/Fir/CUDA/cuda-compiler-generated-names.mlir b/flang/test/Fir/CUDA/cuda-compiler-generated-names.mlir new file mode 100644 index 0000000000000..4507e444d1b51 --- /dev/null +++ b/flang/test/Fir/CUDA/cuda-compiler-generated-names.mlir @@ -0,0 +1,17 @@ +// RUN: fir-opt --split-input-file --compiler-generated-names --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu type-descriptors-renamed-for-assembly=true" %s | FileCheck %s + +module @mod1 attributes {gpu.container} { + gpu.module @gpu1 { + fir.global linkonce @_QMtest_dinitE.dt.tseq constant : i8 + + func.func @embox1(%arg0: !fir.ref>) { + %0 = fir.embox %arg0() : (!fir.ref>) -> !fir.box> + return + } + } +} + +// CHECK-LABEL: gpu.module @gpu1 +// CHECK: llvm.mlir.global linkonce constant @_QMtest_dinitEXdtXtseq +// CHECK: llvm.mlir.addressof @_QMtest_dinitEXdtXtseq : !llvm.ptr +