From cfd4c1805ead139f84a4465719c49cca53f07f27 Mon Sep 17 00:00:00 2001 From: Slava Zakharin Date: Wed, 21 Aug 2024 13:37:03 -0700 Subject: [RFC][flang] Replace special symbols in uniqued global names. (#104859) This change addresses more "issues" as the one resolved in #71338. Some targets (e.g. NVPTX) do not accept global names containing `.`. In particular, the global variables created to represent the runtime information of derived types use `.` in their names. A derived type's descriptor object may be used in the device code, e.g. to initialize a descriptor of a variable of this type. Thus, the runtime type info objects may need to be compiled for the device. Moreover, at least the derived types' descriptor objects may need to be registered (think of `omp declare target`) for the host-device association so that the addendum pointer can be properly mapped to the device for descriptors using a derived type's descriptor as their addendum pointer. The registration implies knowing the name of the global variable in the device image so that proper host code can be created. So it is better to name the globals the same way for the host and the device. CompilerGeneratedNamesConversion pass renames all uniqued globals such that the special symbols (currently `.`) are replaced with `X`. The pass is supposed to be run for the host and the device. An option is added to FIR-to-LLVM conversion pass to indicate whether the new pass has been run before or not. This setting affects how the codegen computes the names of the derived types' descriptors for FIR derived types. fir::NameUniquer now allows `X` to be part of a name, because the name deconstruction may be applied to the mangled names after CompilerGeneratedNamesConversion pass. --- flang/lib/Optimizer/CodeGen/CodeGen.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'flang/lib/Optimizer/CodeGen/CodeGen.cpp') diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 1713cf98a8b9..e419b2612529 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -1201,7 +1201,9 @@ struct EmboxCommonConversion : public fir::FIROpConversion { mlir::Location loc, fir::RecordType recType) const { std::string name = - fir::NameUniquer::getTypeDescriptorName(recType.getName()); + this->options.typeDescriptorsRenamedForAssembly + ? fir::NameUniquer::getTypeDescriptorAssemblyName(recType.getName()) + : fir::NameUniquer::getTypeDescriptorName(recType.getName()); mlir::Type llvmPtrTy = ::getLlvmPtrType(mod.getContext()); if (auto global = mod.template lookupSymbol(name)) { return rewriter.create(loc, llvmPtrTy, @@ -2704,7 +2706,10 @@ struct TypeDescOpConversion : public fir::FIROpConversion { auto recordType = mlir::dyn_cast(inTy); auto module = typeDescOp.getOperation()->getParentOfType(); std::string typeDescName = - fir::NameUniquer::getTypeDescriptorName(recordType.getName()); + this->options.typeDescriptorsRenamedForAssembly + ? fir::NameUniquer::getTypeDescriptorAssemblyName( + recordType.getName()) + : fir::NameUniquer::getTypeDescriptorName(recordType.getName()); auto llvmPtrTy = ::getLlvmPtrType(typeDescOp.getContext()); if (auto global = module.lookupSymbol(typeDescName)) { rewriter.replaceOpWithNewOp( @@ -3653,6 +3658,10 @@ public: if (!forcedTargetFeatures.empty()) fir::setTargetFeatures(mod, forcedTargetFeatures); + if (typeDescriptorsRenamedForAssembly) + options.typeDescriptorsRenamedForAssembly = + typeDescriptorsRenamedForAssembly; + // Run dynamic pass pipeline for converting Math dialect // operations into other dialects (llvm, func, etc.). // Some conversions of Math operations cannot be done -- cgit v1.2.3