[flang-commits] [flang] [flang][debug] Add support for common blocks. (PR #112398)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu Oct 17 04:09:53 PDT 2024


================
@@ -90,6 +105,67 @@ bool debugInfoIsAlreadySet(mlir::Location loc) {
 
 } // namespace
 
+bool AddDebugInfoPass::createCommonBlockGlobal(
+    fir::cg::XDeclareOp declOp, const std::string &name,
+    mlir::LLVM::DIFileAttr fileAttr, mlir::LLVM::DIScopeAttr scopeAttr,
+    fir::DebugTypeGenerator &typeGen, mlir::SymbolTable *symbolTable) {
+  mlir::MLIRContext *context = &getContext();
+  mlir::OpBuilder builder(context);
+  std::optional<std::int64_t> optint;
+  auto op = declOp.getMemref().getDefiningOp();
+
+  if (auto conOp = mlir::dyn_cast_if_present<fir::ConvertOp>(op))
+    op = conOp.getValue().getDefiningOp();
+
+  if (auto cordOp = mlir::dyn_cast_if_present<fir::CoordinateOp>(op)) {
+    optint = fir::getIntIfConstant(cordOp.getOperand(1));
+    if (!optint)
+      return false;
+    op = cordOp.getRef().getDefiningOp();
+    if (auto conOp2 = mlir::dyn_cast_if_present<fir::ConvertOp>(op))
+      op = conOp2.getValue().getDefiningOp();
+
+    if (auto addrOfOp = mlir::dyn_cast_if_present<fir::AddrOfOp>(op)) {
+      auto sym = addrOfOp.getSymbol();
+      if (auto global =
+              symbolTable->lookup<fir::GlobalOp>(sym.getRootReference())) {
+
+        unsigned line = getLineFromLoc(global.getLoc());
+        std::string commonName(sym.getRootReference().str());
----------------
tblah wrote:

ultra-nit: Creating a `std::string` leads to unnecessary heap allocation. A `SymbolRefAttr` can also be converted into an `llvm::StringRef`, which can then be used as an argument type to `getOrCreateCommonBlockAttr`.

`StringRef::drop_back` just creates a `StringRef` with a shorter length. It does not modify the source buffer.

https://github.com/llvm/llvm-project/pull/112398


More information about the flang-commits mailing list