2024-04-15 10:18:31 +01:00
|
|
|
//===-------------- AddDebugInfo.cpp -- add debug info -------------------===//
|
2023-01-12 10:34:34 +00:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
/// \file
|
|
|
|
|
/// This pass populates some debug information for the module and functions.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2024-04-30 08:25:54 +01:00
|
|
|
#include "DebugTypeGenerator.h"
|
2024-04-23 19:11:43 +01:00
|
|
|
#include "flang/Common/Version.h"
|
2023-01-12 10:34:34 +00:00
|
|
|
#include "flang/Optimizer/Builder/FIRBuilder.h"
|
|
|
|
|
#include "flang/Optimizer/Builder/Todo.h"
|
2024-05-16 09:10:59 +01:00
|
|
|
#include "flang/Optimizer/CodeGen/CGOps.h"
|
2023-01-12 10:34:34 +00:00
|
|
|
#include "flang/Optimizer/Dialect/FIRDialect.h"
|
|
|
|
|
#include "flang/Optimizer/Dialect/FIROps.h"
|
|
|
|
|
#include "flang/Optimizer/Dialect/FIRType.h"
|
2023-03-08 18:39:40 -08:00
|
|
|
#include "flang/Optimizer/Dialect/Support/FIRContext.h"
|
2024-04-23 19:11:43 +01:00
|
|
|
#include "flang/Optimizer/Support/InternalNames.h"
|
2023-01-12 10:34:34 +00:00
|
|
|
#include "flang/Optimizer/Transforms/Passes.h"
|
|
|
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
|
|
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
|
|
|
#include "mlir/IR/Matchers.h"
|
|
|
|
|
#include "mlir/IR/TypeUtilities.h"
|
|
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
|
#include "mlir/Transforms/RegionUtils.h"
|
|
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
|
|
|
|
#include "llvm/Support/Debug.h"
|
2024-04-23 19:11:43 +01:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2023-01-12 10:34:34 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
|
|
namespace fir {
|
2024-04-15 10:18:31 +01:00
|
|
|
#define GEN_PASS_DEF_ADDDEBUGINFO
|
2023-01-12 10:34:34 +00:00
|
|
|
#include "flang/Optimizer/Transforms/Passes.h.inc"
|
|
|
|
|
} // namespace fir
|
|
|
|
|
|
2024-04-18 13:54:53 +01:00
|
|
|
#define DEBUG_TYPE "flang-add-debug-info"
|
2023-01-12 10:34:34 +00:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2024-04-15 10:18:31 +01:00
|
|
|
class AddDebugInfoPass : public fir::impl::AddDebugInfoBase<AddDebugInfoPass> {
|
2024-05-16 09:10:59 +01:00
|
|
|
void handleDeclareOp(fir::cg::XDeclareOp declOp,
|
|
|
|
|
mlir::LLVM::DIFileAttr fileAttr,
|
|
|
|
|
mlir::LLVM::DIScopeAttr scopeAttr,
|
|
|
|
|
fir::DebugTypeGenerator &typeGen);
|
|
|
|
|
|
2023-01-12 10:34:34 +00:00
|
|
|
public:
|
2024-04-23 19:11:43 +01:00
|
|
|
AddDebugInfoPass(fir::AddDebugInfoOptions options) : Base(options) {}
|
2023-01-12 10:34:34 +00:00
|
|
|
void runOnOperation() override;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-16 09:10:59 +01:00
|
|
|
static uint32_t getLineFromLoc(mlir::Location loc) {
|
|
|
|
|
uint32_t line = 1;
|
|
|
|
|
if (auto fileLoc = mlir::dyn_cast<mlir::FileLineColLoc>(loc))
|
|
|
|
|
line = fileLoc.getLine();
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 10:34:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2024-05-16 09:10:59 +01:00
|
|
|
void AddDebugInfoPass::handleDeclareOp(fir::cg::XDeclareOp declOp,
|
|
|
|
|
mlir::LLVM::DIFileAttr fileAttr,
|
|
|
|
|
mlir::LLVM::DIScopeAttr scopeAttr,
|
|
|
|
|
fir::DebugTypeGenerator &typeGen) {
|
|
|
|
|
mlir::MLIRContext *context = &getContext();
|
|
|
|
|
mlir::OpBuilder builder(context);
|
|
|
|
|
auto result = fir::NameUniquer::deconstruct(declOp.getUniqName());
|
|
|
|
|
|
|
|
|
|
if (result.first != fir::NameUniquer::NameKind::VARIABLE)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Only accept local variables.
|
|
|
|
|
if (result.second.procs.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// FIXME: There may be cases where an argument is processed a bit before
|
|
|
|
|
// DeclareOp is generated. In that case, DeclareOp may point to an
|
|
|
|
|
// intermediate op and not to BlockArgument. We need to find those cases and
|
|
|
|
|
// walk the chain to get to the actual argument.
|
|
|
|
|
|
|
|
|
|
unsigned argNo = 0;
|
|
|
|
|
if (auto Arg = llvm::dyn_cast<mlir::BlockArgument>(declOp.getMemref()))
|
|
|
|
|
argNo = Arg.getArgNumber() + 1;
|
|
|
|
|
|
|
|
|
|
auto tyAttr = typeGen.convertType(fir::unwrapRefType(declOp.getType()),
|
|
|
|
|
fileAttr, scopeAttr, declOp.getLoc());
|
|
|
|
|
|
|
|
|
|
auto localVarAttr = mlir::LLVM::DILocalVariableAttr::get(
|
|
|
|
|
context, scopeAttr, mlir::StringAttr::get(context, result.second.name),
|
|
|
|
|
fileAttr, getLineFromLoc(declOp.getLoc()), argNo, /* alignInBits*/ 0,
|
|
|
|
|
tyAttr);
|
|
|
|
|
declOp->setLoc(builder.getFusedLoc({declOp->getLoc()}, localVarAttr));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 10:18:31 +01:00
|
|
|
void AddDebugInfoPass::runOnOperation() {
|
2023-01-12 10:34:34 +00:00
|
|
|
mlir::ModuleOp module = getOperation();
|
|
|
|
|
mlir::MLIRContext *context = &getContext();
|
|
|
|
|
mlir::OpBuilder builder(context);
|
2024-04-23 19:11:43 +01:00
|
|
|
llvm::StringRef fileName;
|
|
|
|
|
std::string filePath;
|
|
|
|
|
// We need 2 type of file paths here.
|
|
|
|
|
// 1. Name of the file as was presented to compiler. This can be absolute
|
|
|
|
|
// or relative to 2.
|
|
|
|
|
// 2. Current working directory
|
|
|
|
|
//
|
|
|
|
|
// We are also dealing with 2 different situations below. One is normal
|
|
|
|
|
// compilation where we will have a value in 'inputFilename' and we can
|
|
|
|
|
// obtain the current directory using 'current_path'.
|
|
|
|
|
// The 2nd case is when this pass is invoked directly from 'fir-opt' tool.
|
|
|
|
|
// In that case, 'inputFilename' may be empty. Location embedded in the
|
|
|
|
|
// module will be used to get file name and its directory.
|
|
|
|
|
if (inputFilename.empty()) {
|
2024-04-28 22:01:42 +02:00
|
|
|
if (auto fileLoc = mlir::dyn_cast<mlir::FileLineColLoc>(module.getLoc())) {
|
2024-04-23 19:11:43 +01:00
|
|
|
fileName = llvm::sys::path::filename(fileLoc.getFilename().getValue());
|
|
|
|
|
filePath = llvm::sys::path::parent_path(fileLoc.getFilename().getValue());
|
|
|
|
|
} else
|
|
|
|
|
fileName = "-";
|
|
|
|
|
} else {
|
|
|
|
|
fileName = inputFilename;
|
|
|
|
|
llvm::SmallString<256> cwd;
|
|
|
|
|
if (!llvm::sys::fs::current_path(cwd))
|
|
|
|
|
filePath = cwd.str();
|
|
|
|
|
}
|
2023-01-12 10:34:34 +00:00
|
|
|
|
2024-04-23 19:11:43 +01:00
|
|
|
mlir::LLVM::DIFileAttr fileAttr =
|
|
|
|
|
mlir::LLVM::DIFileAttr::get(context, fileName, filePath);
|
|
|
|
|
mlir::StringAttr producer =
|
|
|
|
|
mlir::StringAttr::get(context, Fortran::common::getFlangFullVersion());
|
2023-01-12 10:34:34 +00:00
|
|
|
mlir::LLVM::DICompileUnitAttr cuAttr = mlir::LLVM::DICompileUnitAttr::get(
|
2024-04-09 06:18:07 -07:00
|
|
|
mlir::DistinctAttr::create(mlir::UnitAttr::get(context)),
|
2024-01-08 07:42:33 +01:00
|
|
|
llvm::dwarf::getLanguage("DW_LANG_Fortran95"), fileAttr, producer,
|
2024-04-23 19:11:43 +01:00
|
|
|
isOptimized, debugLevel);
|
2023-01-25 13:37:54 +00:00
|
|
|
|
2023-01-12 10:34:34 +00:00
|
|
|
module.walk([&](mlir::func::FuncOp funcOp) {
|
2023-01-25 13:37:54 +00:00
|
|
|
mlir::Location l = funcOp->getLoc();
|
|
|
|
|
// If fused location has already been created then nothing to do
|
|
|
|
|
// Otherwise, create a fused location.
|
2024-04-28 22:01:42 +02:00
|
|
|
if (mlir::dyn_cast<mlir::FusedLoc>(l))
|
2023-01-25 13:37:54 +00:00
|
|
|
return;
|
|
|
|
|
|
2024-04-23 19:11:43 +01:00
|
|
|
unsigned int CC = (funcOp.getName() == fir::NameUniquer::doProgramEntry())
|
|
|
|
|
? llvm::dwarf::getCallingConvention("DW_CC_program")
|
|
|
|
|
: llvm::dwarf::getCallingConvention("DW_CC_normal");
|
|
|
|
|
|
2024-04-28 22:01:42 +02:00
|
|
|
if (auto funcLoc = mlir::dyn_cast<mlir::FileLineColLoc>(l)) {
|
2024-04-23 19:11:43 +01:00
|
|
|
fileName = llvm::sys::path::filename(funcLoc.getFilename().getValue());
|
|
|
|
|
filePath = llvm::sys::path::parent_path(funcLoc.getFilename().getValue());
|
|
|
|
|
}
|
2023-01-25 13:37:54 +00:00
|
|
|
|
2024-04-30 08:25:54 +01:00
|
|
|
mlir::StringAttr fullName =
|
2024-04-29 11:02:16 +01:00
|
|
|
mlir::StringAttr::get(context, funcOp.getName());
|
2024-04-30 08:25:54 +01:00
|
|
|
auto result = fir::NameUniquer::deconstruct(funcOp.getName());
|
|
|
|
|
mlir::StringAttr funcName =
|
|
|
|
|
mlir::StringAttr::get(context, result.second.name);
|
|
|
|
|
|
|
|
|
|
llvm::SmallVector<mlir::LLVM::DITypeAttr> types;
|
|
|
|
|
fir::DebugTypeGenerator typeGen(module);
|
|
|
|
|
for (auto resTy : funcOp.getResultTypes()) {
|
|
|
|
|
auto tyAttr =
|
|
|
|
|
typeGen.convertType(resTy, fileAttr, cuAttr, funcOp.getLoc());
|
|
|
|
|
types.push_back(tyAttr);
|
|
|
|
|
}
|
|
|
|
|
for (auto inTy : funcOp.getArgumentTypes()) {
|
|
|
|
|
auto tyAttr = typeGen.convertType(fir::unwrapRefType(inTy), fileAttr,
|
|
|
|
|
cuAttr, funcOp.getLoc());
|
|
|
|
|
types.push_back(tyAttr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 10:34:34 +00:00
|
|
|
mlir::LLVM::DISubroutineTypeAttr subTypeAttr =
|
2024-04-30 08:25:54 +01:00
|
|
|
mlir::LLVM::DISubroutineTypeAttr::get(context, CC, types);
|
2024-04-23 19:11:43 +01:00
|
|
|
mlir::LLVM::DIFileAttr funcFileAttr =
|
|
|
|
|
mlir::LLVM::DIFileAttr::get(context, fileName, filePath);
|
2024-01-08 08:25:30 +01:00
|
|
|
|
|
|
|
|
// Only definitions need a distinct identifier and a compilation unit.
|
|
|
|
|
mlir::DistinctAttr id;
|
|
|
|
|
mlir::LLVM::DICompileUnitAttr compilationUnit;
|
2024-04-23 19:11:43 +01:00
|
|
|
mlir::LLVM::DISubprogramFlags subprogramFlags =
|
|
|
|
|
mlir::LLVM::DISubprogramFlags{};
|
|
|
|
|
if (isOptimized)
|
|
|
|
|
subprogramFlags = mlir::LLVM::DISubprogramFlags::Optimized;
|
2024-01-08 08:25:30 +01:00
|
|
|
if (!funcOp.isExternal()) {
|
|
|
|
|
id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
|
|
|
|
|
compilationUnit = cuAttr;
|
2024-01-15 07:34:13 +01:00
|
|
|
subprogramFlags =
|
|
|
|
|
subprogramFlags | mlir::LLVM::DISubprogramFlags::Definition;
|
2024-01-08 08:25:30 +01:00
|
|
|
}
|
2024-05-16 09:10:59 +01:00
|
|
|
unsigned line = getLineFromLoc(l);
|
2024-01-15 07:34:13 +01:00
|
|
|
auto spAttr = mlir::LLVM::DISubprogramAttr::get(
|
2024-04-30 08:25:54 +01:00
|
|
|
context, id, compilationUnit, fileAttr, funcName, fullName,
|
|
|
|
|
funcFileAttr, line, line, subprogramFlags, subTypeAttr);
|
2023-01-12 10:34:34 +00:00
|
|
|
funcOp->setLoc(builder.getFusedLoc({funcOp->getLoc()}, spAttr));
|
2024-05-16 09:10:59 +01:00
|
|
|
|
|
|
|
|
// Don't process variables if user asked for line tables only.
|
|
|
|
|
if (debugLevel == mlir::LLVM::DIEmissionKind::LineTablesOnly)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
funcOp.walk([&](fir::cg::XDeclareOp declOp) {
|
|
|
|
|
handleDeclareOp(declOp, fileAttr, spAttr, typeGen);
|
|
|
|
|
});
|
2023-01-12 10:34:34 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 19:11:43 +01:00
|
|
|
std::unique_ptr<mlir::Pass>
|
|
|
|
|
fir::createAddDebugInfoPass(fir::AddDebugInfoOptions options) {
|
|
|
|
|
return std::make_unique<AddDebugInfoPass>(options);
|
2023-01-12 10:34:34 +00:00
|
|
|
}
|