Skip to content

Commit

Permalink
[LLVM] Update creation of llvm::DebugLoc, remove TVM_LLVM_VERSION < 70 (
Browse files Browse the repository at this point in the history
#12069)

* [LLVM] Update creation of llvm::DebugLoc, remove TVM_LLVM_VERSION < 70

* Properly deal with "handle" type

* Emit correct subroutine flags

* Fix llvm testcase to account for presence of debug metadata
  • Loading branch information
Krzysztof Parzyszek authored Jul 13, 2022
1 parent 3992d24 commit ca88c52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
27 changes: 18 additions & 9 deletions src/target/llvm/codegen_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void CodeGenCPU::AddFunction(const PrimFunc& f) {

// Following Glow |DebugInfo::generateFunctionDebugInfo|, https://git.io/fjadv
void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
#if TVM_LLVM_VERSION >= 50 && TVM_LLVM_VERSION < 70
#if TVM_LLVM_VERSION >= 50
ICHECK(!f_llvm->getSubprogram());
llvm::SmallVector<llvm::Metadata*, 4> paramTys;
// Functions in TIR can only return void or an int.
Expand All @@ -213,16 +213,20 @@ void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
auto* DIFunctionTy = dbg_info_->di_builder_->createSubroutineType(
dbg_info_->di_builder_->getOrCreateTypeArray(paramTys));

bool local_to_unit = llvm::GlobalValue::isLocalLinkage(f_llvm->getLinkage());

#if TVM_LLVM_VERSION >= 80
auto SPFlags =
llvm::DISubprogram::toSPFlags(local_to_unit, /*IsDefinition=*/true, /*IsOptimized=*/true);
auto* DIFunction = dbg_info_->di_builder_->createFunction(
/*Scope=*/dbg_info_->file_, /*Name=*/f_llvm->getName(), /*LinkageName=*/"",
/*File=*/dbg_info_->file_, /*LineNo=*/0, /*Ty=*/DIFunctionTy,
/*ScopeLine=*/0);
/*ScopeLine=*/0, /*Flags=*/llvm::DINode::FlagZero, /*SPFlags=*/SPFlags);
#else
auto* DIFunction = dbg_info_->di_builder_->createFunction(
/*Scope=*/dbg_info_->file_, /*Name=*/f_llvm->getName(), /*LinkageName=*/"",
/*File=*/dbg_info_->file_, /*LineNo=*/0, /*Ty=*/DIFunctionTy,
/*isLocalToUnit=*/false, /*isDefinition=*/true, /*ScopeLine=*/0,
/*isLocalToUnit=*/local_to_unit, /*isDefinition=*/true, /*ScopeLine=*/0,
/*Flags=*/llvm::DINode::FlagPrototyped, /*isOptimized=*/true);
#endif

Expand All @@ -244,9 +248,10 @@ void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
GetDebugType(GetType(f_tir->params[i]), f_llvm->getFunctionType()->getParamType(i)),
/*alwaysPreserve=*/true);
auto* store = builder.CreateStore(f_llvm->arg_begin() + i, paramAlloca);
auto* di_loc = llvm::DILocation::get(*ctx_, 0, 0, DIFunction);
dbg_info_->di_builder_->insertDeclare(paramAlloca, param,
dbg_info_->di_builder_->createExpression(),
llvm::DebugLoc::get(0, 0, DIFunction), store);
llvm::DebugLoc(di_loc), store);
}
dbg_info_->di_builder_->finalizeSubprogram(f_llvm->getSubprogram());
auto* scope = f_llvm->getSubprogram();
Expand All @@ -258,7 +263,8 @@ void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
if (I.getDebugLoc()) {
continue;
}
I.setDebugLoc(llvm::DebugLoc::get(0, 0, scope));
auto* di_loc = llvm::DILocation::get(*ctx_, 0, 0, scope);
I.setDebugLoc(llvm::DebugLoc(di_loc));
}
}
#endif
Expand All @@ -275,10 +281,13 @@ llvm::DIType* CodeGenCPU::GetDebugType(const Type& ty_tir, llvm::Type* ty_llvm)
return dbg_info_->di_builder_->createBasicType("int32", 32, llvm::dwarf::DW_ATE_signed);
} else if (ty_llvm->isPointerTy()) {
auto* ptr_type = ty_tir.as<PointerTypeNode>();
ICHECK(ptr_type != nullptr) << "Got LLVM pointer type from non-pointer IR type: " << ty_tir;
Type elem_type = ptr_type->element_type;
return dbg_info_->di_builder_->createPointerType(
GetDebugType(elem_type, GetLLVMType(elem_type)), ty_llvm->getPrimitiveSizeInBits());
ICHECK(ptr_type != nullptr || GetRuntimeDataType(ty_tir).is_handle())
<< "Got LLVM pointer type from non-pointer IR type: " << ty_tir;
auto* pointee_type = ptr_type != nullptr ? GetDebugType(ptr_type->element_type,
GetLLVMType(ptr_type->element_type))
: nullptr;
return dbg_info_->di_builder_->createPointerType(pointee_type,
ty_llvm->getPrimitiveSizeInBits());
} else {
std::string type_str;
llvm::raw_string_ostream rso(type_str);
Expand Down
6 changes: 4 additions & 2 deletions tests/python/unittest/test_target_codegen_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,9 @@ def test_llvm_target_attributes():
functions_with_target = []

for line in llvm_ir_lines:
func_def = re.match("define.* @(?P<func_name>[^(]*)\(.* #(?P<attr_num>[0-9]+) {$", line)
func_def = re.match(
"define.* @(?P<func_name>[^(]*)[(].* #(?P<attr_num>[0-9]+) (!.* |){$", line
)
if func_def:
functions_with_target.append(func_def.group("func_name"))
attributes_with_target[func_def.group("attr_num")] = True
Expand All @@ -969,7 +971,7 @@ def test_llvm_target_attributes():

for k in list(attributes_with_target.keys()):
assert re.match('.*"target-cpu"="skylake".*', attribute_definitions[k])
assert re.match('.*"target-features"=".*\+avx512f.*".*', attribute_definitions[k])
assert re.match('.*"target-features"=".*[+]avx512f.*".*', attribute_definitions[k])

expected_functions = ["test_func", "test_func_compute_", "__tvm_parallel_lambda"]
for n in expected_functions:
Expand Down

0 comments on commit ca88c52

Please sign in to comment.