[flang-commits] [flang] [flang] Represent use statement in fir. (PR #168106)
Abid Qadeer via flang-commits
flang-commits at lists.llvm.org
Mon Dec 15 06:29:12 PST 2025
================
@@ -226,6 +226,95 @@ static mlir::FlatSymbolRefAttr gatherComponentInit(
return mlir::FlatSymbolRefAttr::get(mlirContext, name);
}
+/// Emit fir.use_stmt operations for USE statements in the given scope
+static void
+emitUseStatementsFromScope(Fortran::lower::AbstractConverter &converter,
+ mlir::OpBuilder &builder, mlir::Location loc,
+ const Fortran::semantics::Scope &scope) {
+ mlir::MLIRContext *context = builder.getContext();
+
+ for (const auto &preservedStmt : scope.preservedUseStmts()) {
+
+ auto getMangledName = [&](const std::string &localName) -> std::string {
+ Fortran::parser::CharBlock charBlock{localName.data(), localName.size()};
+ const auto *sym = scope.FindSymbol(charBlock);
+ if (!sym)
+ return "";
+
+ const auto &ultimateSym = sym->GetUltimate();
+
+ // Skip cases which can cause mangleName to fail.
+ if (ultimateSym.has<Fortran::semantics::DerivedTypeDetails>())
+ return "";
+
+ if (const auto *generic =
+ ultimateSym.detailsIf<Fortran::semantics::GenericDetails>()) {
+ if (!generic->specific())
+ return "";
+ }
+
+ return converter.mangleName(ultimateSym);
+ };
+
+ mlir::StringAttr moduleNameAttr =
+ mlir::StringAttr::get(context, preservedStmt.moduleName);
+
+ llvm::SmallVector<mlir::Attribute> onlySymbolAttrs;
+ llvm::SmallVector<mlir::Attribute> renameAttrs;
+
+ switch (preservedStmt.kind) {
+ case Fortran::semantics::PreservedUseStmt::Kind::UseOnly:
+ // USE mod, ONLY: list
+ for (const auto &name : preservedStmt.onlyNames) {
+ std::string mangledName = getMangledName(name);
+ if (!mangledName.empty())
+ onlySymbolAttrs.push_back(
+ mlir::FlatSymbolRefAttr::get(context, mangledName));
+ }
+ // Handle renames within ONLY clause
+ for (const auto &local : preservedStmt.renames) {
+ std::string mangledName = getMangledName(local);
+ if (!mangledName.empty()) {
+ auto localAttr = mlir::StringAttr::get(context, local);
+ auto symbolRef = mlir::FlatSymbolRefAttr::get(context, mangledName);
+ renameAttrs.push_back(
+ fir::UseRenameAttr::get(context, localAttr, symbolRef));
+ }
+ }
+ break;
+
+ case Fortran::semantics::PreservedUseStmt::Kind::UseRenames:
+ // USE mod, renames (import all with some renames)
+ for (const auto &local : preservedStmt.renames) {
+ std::string mangledName = getMangledName(local);
+ if (!mangledName.empty()) {
+ auto localAttr = mlir::StringAttr::get(context, local);
+ auto symbolRef = mlir::FlatSymbolRefAttr::get(context, mangledName);
+ renameAttrs.push_back(
+ fir::UseRenameAttr::get(context, localAttr, symbolRef));
+ }
+ }
+ break;
+
+ case Fortran::semantics::PreservedUseStmt::Kind::UseAll:
+ // USE mod (import all, no renames)
+ break;
+ }
----------------
abidh wrote:
I have simplified the code as suggested. Please let me know if you have any other comments.
https://github.com/llvm/llvm-project/pull/168106
More information about the flang-commits
mailing list