Skip to content

Commit ecda140

Browse files
authored
[flang][cuda] Adapt ExternalNameConversion to work in gpu module (#117039)
1 parent 668f2c7 commit ecda140

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp

+29-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
1313
#include "flang/Optimizer/Support/InternalNames.h"
1414
#include "flang/Optimizer/Transforms/Passes.h"
15+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
1516
#include "mlir/IR/Attributes.h"
1617
#include "mlir/IR/SymbolTable.h"
1718
#include "mlir/Pass/Pass.h"
@@ -58,26 +59,36 @@ void ExternalNameConversionPass::runOnOperation() {
5859
auto *context = &getContext();
5960

6061
llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> remappings;
61-
// Update names of external Fortran functions and names of Common Block
62-
// globals.
63-
for (auto &funcOrGlobal : op->getRegion(0).front()) {
64-
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal) ||
65-
llvm::isa<fir::GlobalOp>(funcOrGlobal)) {
66-
auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>(
67-
mlir::SymbolTable::getSymbolAttrName());
68-
auto deconstructedName = fir::NameUniquer::deconstruct(symName);
69-
if (fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) {
70-
auto newName =
71-
mangleExternalName(deconstructedName, appendUnderscoreOpt);
72-
auto newAttr = mlir::StringAttr::get(context, newName);
73-
mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr);
74-
auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr);
75-
remappings.try_emplace(symName, newSymRef);
76-
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal))
77-
funcOrGlobal.setAttr(fir::getInternalFuncNameAttrName(), symName);
62+
63+
auto renameFuncOrGlobalInModule = [&](mlir::Operation *module) {
64+
for (auto &funcOrGlobal : module->getRegion(0).front()) {
65+
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal) ||
66+
llvm::isa<fir::GlobalOp>(funcOrGlobal)) {
67+
auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>(
68+
mlir::SymbolTable::getSymbolAttrName());
69+
auto deconstructedName = fir::NameUniquer::deconstruct(symName);
70+
if (fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) {
71+
auto newName =
72+
mangleExternalName(deconstructedName, appendUnderscoreOpt);
73+
auto newAttr = mlir::StringAttr::get(context, newName);
74+
mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr);
75+
auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr);
76+
remappings.try_emplace(symName, newSymRef);
77+
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal))
78+
funcOrGlobal.setAttr(fir::getInternalFuncNameAttrName(), symName);
79+
}
7880
}
7981
}
80-
}
82+
};
83+
84+
// Update names of external Fortran functions and names of Common Block
85+
// globals.
86+
renameFuncOrGlobalInModule(op);
87+
88+
// Do the same in GPU modules.
89+
if (auto mod = mlir::dyn_cast_or_null<mlir::ModuleOp>(*op))
90+
for (auto gpuMod : mod.getOps<mlir::gpu::GPUModuleOp>())
91+
renameFuncOrGlobalInModule(gpuMod);
8192

8293
if (remappings.empty())
8394
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: fir-opt --split-input-file --external-name-interop %s | FileCheck %s
2+
3+
gpu.module @cuda_device_mod {
4+
gpu.func @_QPfoo() {
5+
fir.call @_QPthreadfence() fastmath<contract> : () -> ()
6+
gpu.return
7+
}
8+
func.func private @_QPthreadfence() attributes {cuf.proc_attr = #cuf.cuda_proc<device>}
9+
}
10+
11+
// CHECK-LABEL: gpu.func @_QPfoo
12+
// CHECK: fir.call @threadfence_()
13+
// CHECK: func.func private @threadfence_()

0 commit comments

Comments
 (0)