Skip to content

Commit 3d944dd

Browse files
authored
Add function signature to code_native and code_llvm (#50630)
When getting LLVM IR from `@code_llvm`, it's sometimes necessary to know exactly which `f` is being compiled. This prints the type signature of a function in front of `code_llvm` and `code_native` to assist in that process. <details> <summary>Example</summary> ```julia julia> @code_llvm zeros(64) ``` ```llvm ; Function Signature: zeros(Int64) ; @ array.jl:629 within `zeros` define nonnull {}* @julia_zeros_121(i64 signext %"dims[1]::Int64") #0 { top: ; @ array.jl:629 within `zeros` @ array.jl:631 @ array.jl:634 ; ┌ @ boot.jl:484 within `Array` @ boot.jl:475 %0 = call nonnull {}* inttoptr (i64 140604991500960 to {}* ({}*, i64)*)({}* inttoptr (i64 140604630439728 to {}*), i64 %"dims[1]::Int64") ; └ ; @ array.jl:629 within `zeros` @ array.jl:631 @ array.jl:635 ; ┌ @ array.jl:392 within `fill!` ; │┌ @ abstractarray.jl:318 within `eachindex` ; ││┌ @ abstractarray.jl:134 within `axes1` ; │││┌ @ abstractarray.jl:98 within `axes` ; ││││┌ @ array.jl:191 within `size` %1 = bitcast {}* %0 to { i8*, i64, i16, i16, i32 }* %.length_ptr = getelementptr inbounds { i8*, i64, i16, i16, i32 }, { i8*, i64, i16, i16, i32 }* %1, i64 0, i32 1 %.length = load i64, i64* %.length_ptr, align 8 ; │└└└└ ; │┌ @ range.jl:897 within `iterate` ; ││┌ @ range.jl:674 within `isempty` ; │││┌ @ operators.jl:378 within `>` ; ││││┌ @ int.jl:83 within `<` %.not.not = icmp eq i64 %.length, 0 ; │└└└└ br i1 %.not.not, label %L30, label %L13.preheader L13.preheader: ; preds = %top %2 = bitcast {}* %0 to i8** %.data1013 = load i8*, i8** %2, align 8 ; │ @ array.jl:394 within `fill!` %3 = shl nuw i64 %.length, 3 ; │ @ array.jl:393 within `fill!` ; │┌ @ array.jl:1019 within `setindex!` call void @llvm.memset.p0i8.i64(i8* nonnull align 8 %.data1013, i8 0, i64 %3, i1 false) ; └└ ; @ array.jl:629 within `zeros` br label %L30 L30: ; preds = %L13.preheader, %top ret {}* %0 } ``` </details>
2 parents 092231c + 90c0e19 commit 3d944dd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/codegen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7447,6 +7447,14 @@ static jl_llvm_functions_t
74477447
declarations.functionObject = needsparams ? "jl_fptr_sparam" : "jl_fptr_args";
74487448
}
74497449

7450+
if (ctx.emission_context.debug_level >= 2 && lam->def.method && jl_is_method(lam->def.method) && lam->specTypes != (jl_value_t*)jl_emptytuple_type) {
7451+
ios_t sigbuf;
7452+
ios_mem(&sigbuf, 0);
7453+
jl_static_show_func_sig((JL_STREAM*) &sigbuf, (jl_value_t*)lam->specTypes);
7454+
f->addFnAttr("julia.fsig", StringRef(sigbuf.buf, sigbuf.size));
7455+
ios_close(&sigbuf);
7456+
}
7457+
74507458
AttrBuilder FnAttrs(ctx.builder.getContext(), f->getAttributes().getFnAttrs());
74517459
AttrBuilder RetAttrs(ctx.builder.getContext(), f->getAttributes().getRetAttrs());
74527460

src/disasm.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ class LineNumberAnnotatedWriter : public AssemblyAnnotationWriter {
363363
void LineNumberAnnotatedWriter::emitFunctionAnnot(
364364
const Function *F, formatted_raw_ostream &Out)
365365
{
366+
if (F->hasFnAttribute("julia.fsig")) {
367+
auto sig = F->getFnAttribute("julia.fsig").getValueAsString();
368+
Out << "; Function Signature: " << sig << "\n";
369+
}
366370
InstrLoc = nullptr;
367371
DISubprogram *FuncLoc = F->getSubprogram();
368372
if (!FuncLoc) {

0 commit comments

Comments
 (0)