#include "DebugTranslation.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
using namespace mlir;
using namespace mlir::LLVM;
using namespace mlir::LLVM::detail;
static WalkResult interruptIfValidLocation(Operation *op) {
return op->getLoc().isa<UnknownLoc>() ? WalkResult::advance()
: WalkResult::interrupt();
}
DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
: builder(llvmModule), llvmCtx(llvmModule.getContext()),
compileUnit(nullptr) {
if (!module->walk(interruptIfValidLocation).wasInterrupted())
return;
compileUnit = builder.createCompileUnit(
llvm::dwarf::DW_LANG_C,
builder.createFile(llvmModule.getModuleIdentifier(), "/"),
"mlir", true, "", 0);
StringRef debugVersionKey = "Debug Info Version";
if (!llvmModule.getModuleFlag(debugVersionKey))
llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
llvm::DEBUG_METADATA_VERSION);
if (auto targetTripleAttr =
module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
auto targetTriple =
llvm::Triple(targetTripleAttr.cast<StringAttr>().getValue());
if (targetTriple.isKnownWindowsMSVCEnvironment()) {
llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
}
}
}
void DebugTranslation::finalize() { builder.finalize(); }
static FileLineColLoc extractFileLoc(Location loc) {
if (auto fileLoc = loc.dyn_cast<FileLineColLoc>())
return fileLoc;
if (auto nameLoc = loc.dyn_cast<NameLoc>())
return extractFileLoc(nameLoc.getChildLoc());
if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>())
return extractFileLoc(opaqueLoc.getFallbackLocation());
return FileLineColLoc();
}
void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted())
return;
const bool hasCallWithoutDebugInfo =
func.walk([&](LLVM::CallOp call) {
return call.getLoc()->walk([](Location l) {
return l.isa<UnknownLoc>() ? WalkResult::interrupt()
: WalkResult::advance();
});
})
.wasInterrupted();
if (hasCallWithoutDebugInfo)
return;
FileLineColLoc fileLoc = extractFileLoc(func.getLoc());
auto *file =
translateFile(fileLoc ? fileLoc.getFilename().strref() : "<unknown>");
unsigned line = fileLoc ? fileLoc.getLine() : 0;
llvm::DISubroutineType *type =
builder.createSubroutineType(builder.getOrCreateTypeArray(llvm::None));
llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition |
llvm::DISubprogram::SPFlagOptimized;
llvm::DISubprogram *program =
builder.createFunction(compileUnit, func.getName(), func.getName(), file,
line, type, line, llvm::DINode::FlagZero, spFlags);
llvmFunc.setSubprogram(program);
builder.finalizeSubprogram(program);
}
const llvm::DILocation *
DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) {
if (!compileUnit)
return nullptr;
return translateLoc(loc, scope, nullptr);
}
const llvm::DILocation *
DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope,
const llvm::DILocation *inlinedAt) {
if (!scope || loc.isa<UnknownLoc>())
return nullptr;
auto existingIt = locationToLoc.find(std::make_pair(loc, scope));
if (existingIt != locationToLoc.end())
return existingIt->second;
const llvm::DILocation *llvmLoc = nullptr;
if (auto callLoc = loc.dyn_cast<CallSiteLoc>()) {
const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
} else if (auto fileLoc = loc.dyn_cast<FileLineColLoc>()) {
auto *file = translateFile(fileLoc.getFilename());
auto *fileScope = builder.createLexicalBlockFile(scope, file);
llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(),
fileLoc.getColumn(), fileScope,
const_cast<llvm::DILocation *>(inlinedAt));
} else if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
ArrayRef<Location> locations = fusedLoc.getLocations();
llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
for (Location locIt : locations.drop_front()) {
llvmLoc = llvm::DILocation::getMergedLocation(
llvmLoc, translateLoc(locIt, scope, inlinedAt));
}
} else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
} else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
inlinedAt);
} else {
llvm_unreachable("unknown location kind");
}
locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc);
return llvmLoc;
}
llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
auto *&file = fileMap[fileName];
if (file)
return file;
if (currentWorkingDir.empty())
llvm::sys::fs::current_path(currentWorkingDir);
StringRef directory = currentWorkingDir;
SmallString<128> dirBuf;
SmallString<128> fileBuf;
if (llvm::sys::path::is_absolute(fileName)) {
auto fileIt = llvm::sys::path::begin(fileName);
auto fileE = llvm::sys::path::end(fileName);
auto curDirIt = llvm::sys::path::begin(directory);
auto curDirE = llvm::sys::path::end(directory);
for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
llvm::sys::path::append(dirBuf, *curDirIt);
if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
directory = StringRef();
} else {
for (; fileIt != fileE; ++fileIt)
llvm::sys::path::append(fileBuf, *fileIt);
directory = dirBuf;
fileName = fileBuf;
}
}
return (file = builder.createFile(fileName, directory));
}