Skip to content

Commit 474b394

Browse files
committed
Address review comments, plus some minor refactoring.
1 parent 392534f commit 474b394

File tree

7 files changed

+57
-55
lines changed

7 files changed

+57
-55
lines changed

src/codegen-stubs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ JL_DLLEXPORT void jl_get_llvm_external_fns_fallback(void *native_code, arraylist
1818

1919
JL_DLLEXPORT void jl_extern_c_fallback(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name) UNAVAILABLE
2020
JL_DLLEXPORT jl_value_t *jl_dump_method_asm_fallback(jl_method_instance_t *linfo, size_t world,
21-
char raw_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE
21+
char emit_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE
2222
JL_DLLEXPORT jl_value_t *jl_dump_function_ir_fallback(jl_llvmf_dump_t *dump, char strip_ir_metadata, char dump_module, const char *debuginfo) UNAVAILABLE
2323
JL_DLLEXPORT void jl_get_llvmf_defn_fallback(jl_llvmf_dump_t *dump, jl_method_instance_t *linfo, size_t world, char getwrapper, char optimize, const jl_cgparams_t params) UNAVAILABLE
2424

@@ -83,9 +83,9 @@ JL_DLLEXPORT void jl_dump_llvm_opt_fallback(void *s)
8383
{
8484
}
8585

86-
JL_DLLEXPORT jl_value_t *jl_dump_fptr_asm_fallback(uint64_t fptr, char raw_mc, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE
86+
JL_DLLEXPORT jl_value_t *jl_dump_fptr_asm_fallback(uint64_t fptr, char emit_mc, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE
8787

88-
JL_DLLEXPORT jl_value_t *jl_dump_function_asm_fallback(jl_llvmf_dump_t* dump, char raw_mc, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE
88+
JL_DLLEXPORT jl_value_t *jl_dump_function_asm_fallback(jl_llvmf_dump_t* dump, char emit_mc, const char* asm_variant, const char *debuginfo, char binary, char raw) UNAVAILABLE
8989

9090
JL_DLLEXPORT void jl_get_function_id_fallback(void *native_code, jl_code_instance_t *ncode,
9191
int32_t *func_idx, int32_t *specfunc_idx) UNAVAILABLE

src/disasm.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ static uint64_t compute_obj_symsize(object::SectionRef Section, uint64_t offset)
575575

576576
// print a native disassembly for the function starting at fptr
577577
extern "C" JL_DLLEXPORT_CODEGEN
578-
jl_value_t *jl_dump_fptr_asm_impl(uint64_t fptr, char raw_mc, const char* asm_variant, const char *debuginfo, char binary)
578+
jl_value_t *jl_dump_fptr_asm_impl(uint64_t fptr, char emit_mc, const char* asm_variant, const char *debuginfo, char binary)
579579
{
580580
assert(fptr != 0);
581581
std::string code;
@@ -600,7 +600,7 @@ jl_value_t *jl_dump_fptr_asm_impl(uint64_t fptr, char raw_mc, const char* asm_va
600600
return jl_pchar_to_string("", 0);
601601
}
602602

603-
if (raw_mc) {
603+
if (emit_mc) {
604604
return (jl_value_t*)jl_pchar_to_array((char*)fptr, symsize);
605605
}
606606

@@ -1203,7 +1203,7 @@ class LineNumberPrinterHandler : public AsmPrinterHandler {
12031203

12041204
// get a native assembly for llvm::Function
12051205
extern "C" JL_DLLEXPORT_CODEGEN
1206-
jl_value_t *jl_dump_function_asm_impl(jl_llvmf_dump_t* dump, char raw_mc, const char* asm_variant, const char *debuginfo, char binary)
1206+
jl_value_t *jl_dump_function_asm_impl(jl_llvmf_dump_t* dump, char emit_mc, const char* asm_variant, const char *debuginfo, char binary, char raw)
12071207
{
12081208
// precise printing via IR assembler
12091209
SmallVector<char, 4096> ObjBufferSV;
@@ -1218,13 +1218,14 @@ jl_value_t *jl_dump_function_asm_impl(jl_llvmf_dump_t* dump, char raw_mc, const
12181218
f2.deleteBody();
12191219
}
12201220
// add a nounwind attribute to get rid of cfi instructions
1221-
f->addFnAttr(Attribute::NoUnwind);
1221+
if (!raw)
1222+
f->addFnAttr(Attribute::NoUnwind);
12221223
});
12231224
auto TMBase = jl_ExecutionEngine->cloneTargetMachine();
12241225
LLVMTargetMachine *TM = static_cast<LLVMTargetMachine*>(TMBase.get());
12251226
legacy::PassManager PM;
12261227
addTargetPasses(&PM, TM->getTargetTriple(), TM->getTargetIRAnalysis());
1227-
if (raw_mc) {
1228+
if (emit_mc) {
12281229
raw_svector_ostream obj_OS(ObjBufferSV);
12291230
if (TM->addPassesToEmitFile(PM, obj_OS, nullptr, CGFT_ObjectFile, false, nullptr))
12301231
return jl_an_empty_string;

src/jitlayers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,14 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
580580
// get a native disassembly for a compiled method
581581
extern "C" JL_DLLEXPORT_CODEGEN
582582
jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
583-
char raw_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary)
583+
char emit_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary)
584584
{
585585
// printing via disassembly
586586
jl_code_instance_t *codeinst = jl_generate_fptr(mi, world);
587587
if (codeinst) {
588588
uintptr_t fptr = (uintptr_t)jl_atomic_load_acquire(&codeinst->invoke);
589589
if (getwrapper)
590-
return jl_dump_fptr_asm(fptr, raw_mc, asm_variant, debuginfo, binary);
590+
return jl_dump_fptr_asm(fptr, emit_mc, asm_variant, debuginfo, binary);
591591
uintptr_t specfptr = (uintptr_t)jl_atomic_load_relaxed(&codeinst->specptr.fptr);
592592
if (fptr == (uintptr_t)jl_fptr_const_return_addr && specfptr == 0) {
593593
// normally we prevent native code from being generated for these functions,
@@ -635,15 +635,15 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
635635
}
636636
}
637637
if (specfptr != 0)
638-
return jl_dump_fptr_asm(specfptr, raw_mc, asm_variant, debuginfo, binary);
638+
return jl_dump_fptr_asm(specfptr, emit_mc, asm_variant, debuginfo, binary);
639639
}
640640

641641
// whatever, that didn't work - use the assembler output instead
642642
jl_llvmf_dump_t llvmf_dump;
643643
jl_get_llvmf_defn(&llvmf_dump, mi, world, getwrapper, true, jl_default_cgparams);
644644
if (!llvmf_dump.F)
645645
return jl_an_empty_string;
646-
return jl_dump_function_asm(&llvmf_dump, raw_mc, asm_variant, debuginfo, binary);
646+
return jl_dump_function_asm(&llvmf_dump, emit_mc, asm_variant, debuginfo, binary, false);
647647
}
648648

649649
CodeGenOpt::Level CodeGenOptLevelFor(int optlevel)

src/julia_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,11 +1658,11 @@ typedef struct {
16581658
} jl_llvmf_dump_t;
16591659

16601660
JL_DLLIMPORT jl_value_t *jl_dump_method_asm(jl_method_instance_t *linfo, size_t world,
1661-
char raw_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary);
1661+
char emit_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary);
16621662
JL_DLLIMPORT void jl_get_llvmf_defn(jl_llvmf_dump_t* dump, jl_method_instance_t *linfo, size_t world, char getwrapper, char optimize, const jl_cgparams_t params);
1663-
JL_DLLIMPORT jl_value_t *jl_dump_fptr_asm(uint64_t fptr, char raw_mc, const char* asm_variant, const char *debuginfo, char binary);
1663+
JL_DLLIMPORT jl_value_t *jl_dump_fptr_asm(uint64_t fptr, char emit_mc, const char* asm_variant, const char *debuginfo, char binary);
16641664
JL_DLLIMPORT jl_value_t *jl_dump_function_ir(jl_llvmf_dump_t *dump, char strip_ir_metadata, char dump_module, const char *debuginfo);
1665-
JL_DLLIMPORT jl_value_t *jl_dump_function_asm(jl_llvmf_dump_t *dump, char raw_mc, const char* asm_variant, const char *debuginfo, char binary);
1665+
JL_DLLIMPORT jl_value_t *jl_dump_function_asm(jl_llvmf_dump_t *dump, char emit_mc, const char* asm_variant, const char *debuginfo, char binary, char raw);
16661666

16671667
JL_DLLIMPORT void *jl_create_native(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvmmod, const jl_cgparams_t *cgparams, int policy, int imaging_mode, int cache, size_t world);
16681668
JL_DLLIMPORT void jl_dump_native(void *native_code,

stdlib/InteractiveUtils/src/codeview.jl

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,15 @@ const OC_MISMATCH_WARNING =
169169
# Printing code representations in IR and assembly
170170

171171
function _dump_function(@nospecialize(f), @nospecialize(t), native::Bool, wrapper::Bool,
172-
strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol,
173-
optimize::Bool, debuginfo::Symbol, binary::Bool,
174-
safepoints::Bool=false)
172+
raw::Bool, dump_module::Bool, syntax::Symbol,
173+
optimize::Bool, debuginfo::Symbol, binary::Bool)
175174
params = CodegenParams(debug_info_kind=Cint(0),
176-
safepoint_on_entry=safepoints)
177-
_dump_function(f, t, native, wrapper, strip_ir_metadata, dump_module, syntax,
175+
safepoint_on_entry=raw)
176+
_dump_function(f, t, native, wrapper, raw, dump_module, syntax,
178177
optimize, debuginfo, binary, params)
179178
end
180179
function _dump_function(@nospecialize(f), @nospecialize(t), native::Bool, wrapper::Bool,
181-
strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol,
180+
raw::Bool, dump_module::Bool, syntax::Symbol,
182181
optimize::Bool, debuginfo::Symbol, binary::Bool, params::CodegenParams)
183182
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
184183
if isa(f, Core.Builtin)
@@ -189,21 +188,21 @@ function _dump_function(@nospecialize(f), @nospecialize(t), native::Bool, wrappe
189188
if !isa(f, Core.OpaqueClosure)
190189
world = Base.get_world_counter()
191190
match = Base._which(signature_type(f, t); world)
192-
linfo = Core.Compiler.specialize_method(match)
191+
mi = Core.Compiler.specialize_method(match)
193192
# TODO: use jl_is_cacheable_sig instead of isdispatchtuple
194-
isdispatchtuple(linfo.specTypes) || (warning = GENERIC_SIG_WARNING)
193+
isdispatchtuple(mi.specTypes) || (warning = GENERIC_SIG_WARNING)
195194
else
196195
world = UInt64(f.world)
197196
if Core.Compiler.is_source_inferred(f.source.source)
198197
# OC was constructed from inferred source. There's only one
199198
# specialization and we can't infer anything more precise either.
200199
world = f.source.primary_world
201-
linfo = f.source.specializations::Core.MethodInstance
200+
mi = f.source.specializations::Core.MethodInstance
202201
Core.Compiler.hasintersect(typeof(f).parameters[1], t) || (warning = OC_MISMATCH_WARNING)
203202
else
204-
linfo = Core.Compiler.specialize_method(f.source, Tuple{typeof(f.captures), t.parameters...}, Core.svec())
205-
actual = isdispatchtuple(linfo.specTypes)
206-
isdispatchtuple(linfo.specTypes) || (warning = GENERIC_SIG_WARNING)
203+
mi = Core.Compiler.specialize_method(f.source, Tuple{typeof(f.captures), t.parameters...}, Core.svec())
204+
actual = isdispatchtuple(mi.specTypes)
205+
isdispatchtuple(mi.specTypes) || (warning = GENERIC_SIG_WARNING)
207206
end
208207
end
209208
# get the code for it
@@ -218,25 +217,24 @@ function _dump_function(@nospecialize(f), @nospecialize(t), native::Bool, wrappe
218217
end
219218
if dump_module
220219
# we want module metadata, so use LLVM to generate assembly output
221-
str = _dump_function_linfo_native(linfo, world, wrapper, syntax, debuginfo, binary, params)
220+
str = _dump_function_native_assembly(mi, world, wrapper, syntax, debuginfo, binary, raw, params)
222221
else
223-
# if we don't want the module metadata, just disassemble what our JIT has.
224-
# TODO: make it possible to hide the safepoint prologue here too.
225-
# once that works, it would be good to default to dump_module=false
226-
# just like code_llvm does.
227-
str = _dump_function_linfo_native(linfo, world, wrapper, syntax, debuginfo, binary)
222+
# if we don't want the module metadata, just disassemble what our JIT has
223+
str = _dump_function_native_disassembly(mi, world, wrapper, syntax, debuginfo, binary)
228224
end
229225
else
230-
str = _dump_function_linfo_llvm(linfo, world, wrapper, strip_ir_metadata, dump_module, optimize, debuginfo, params)
226+
str = _dump_function_llvm(mi, world, wrapper, !raw, dump_module, optimize, debuginfo, params)
231227
end
232228
str = warning * str
233229
return str
234230
end
235231

236-
function _dump_function_linfo_native(linfo::Core.MethodInstance, world::UInt, wrapper::Bool, syntax::Symbol, debuginfo::Symbol, binary::Bool)
237-
str = ccall(:jl_dump_method_asm, Ref{String},
238-
(Any, UInt, Bool, Bool, Ptr{UInt8}, Ptr{UInt8}, Bool),
239-
linfo, world, false, wrapper, syntax, debuginfo, binary)
232+
function _dump_function_native_disassembly(mi::Core.MethodInstance, world::UInt,
233+
wrapper::Bool, syntax::Symbol,
234+
debuginfo::Symbol, binary::Bool)
235+
str = @ccall jl_dump_method_asm(mi::Any, world::UInt, false::Bool, wrapper::Bool,
236+
syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
237+
wrapper::Bool)::Ref{String}
240238
return str
241239
end
242240

@@ -245,27 +243,30 @@ struct LLVMFDump
245243
f::Ptr{Cvoid} # opaque
246244
end
247245

248-
function _dump_function_linfo_native(linfo::Core.MethodInstance, world::UInt, wrapper::Bool, syntax::Symbol, debuginfo::Symbol, binary::Bool, params::CodegenParams)
246+
function _dump_function_native_assembly(mi::Core.MethodInstance, world::UInt,
247+
wrapper::Bool, syntax::Symbol, debuginfo::Symbol,
248+
binary::Bool, raw::Bool, params::CodegenParams)
249249
llvmf_dump = Ref{LLVMFDump}()
250-
ccall(:jl_get_llvmf_defn, Cvoid, (Ptr{LLVMFDump}, Any, UInt, Bool, Bool, CodegenParams), llvmf_dump, linfo, world, wrapper, true, params)
250+
@ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump},mi::Any, world::UInt, wrapper::Bool,
251+
true::Bool, params::CodegenParams)::Cvoid
251252
llvmf_dump[].f == C_NULL && error("could not compile the specified method")
252-
str = ccall(:jl_dump_function_asm, Ref{String},
253-
(Ptr{LLVMFDump}, Bool, Ptr{UInt8}, Ptr{UInt8}, Bool),
254-
llvmf_dump, false, syntax, debuginfo, binary)
253+
str = @ccall jl_dump_function_asm(llvmf_dump::Ptr{LLVMFDump}, false::Bool,
254+
syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
255+
binary::Bool, raw::Bool)::Ref{String}
255256
return str
256257
end
257258

258-
function _dump_function_linfo_llvm(
259-
linfo::Core.MethodInstance, world::UInt, wrapper::Bool,
259+
function _dump_function_llvm(
260+
mi::Core.MethodInstance, world::UInt, wrapper::Bool,
260261
strip_ir_metadata::Bool, dump_module::Bool,
261262
optimize::Bool, debuginfo::Symbol,
262263
params::CodegenParams)
263264
llvmf_dump = Ref{LLVMFDump}()
264-
ccall(:jl_get_llvmf_defn, Cvoid, (Ptr{LLVMFDump}, Any, UInt, Bool, Bool, CodegenParams), llvmf_dump, linfo, world, wrapper, optimize, params)
265+
@ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump}, mi::Any, world::UInt,
266+
wrapper::Bool, optimize::Bool, params::CodegenParams)::Cvoid
265267
llvmf_dump[].f == C_NULL && error("could not compile the specified method")
266-
str = ccall(:jl_dump_function_ir, Ref{String},
267-
(Ptr{LLVMFDump}, Bool, Bool, Ptr{UInt8}),
268-
llvmf_dump, strip_ir_metadata, dump_module, debuginfo)
268+
str = @ccall jl_dump_function_ir(llvmf_dump::Ptr{LLVMFDump}, strip_ir_metadata::Bool,
269+
dump_module::Bool, debuginfo::Ptr{UInt8})::Ref{String}
269270
return str
270271
end
271272

@@ -282,7 +283,7 @@ Keyword argument `debuginfo` may be one of source (default) or none, to specify
282283
"""
283284
function code_llvm(io::IO, @nospecialize(f), @nospecialize(types), raw::Bool,
284285
dump_module::Bool=false, optimize::Bool=true, debuginfo::Symbol=:default)
285-
d = _dump_function(f, types, false, false, !raw, dump_module, :intel, optimize, debuginfo, false, raw)
286+
d = _dump_function(f, types, false, false, raw, dump_module, :intel, optimize, debuginfo, false)
286287
if highlighting[:llvm] && get(io, :color, false)::Bool
287288
print_llvm(io, d)
288289
else
@@ -304,14 +305,14 @@ generic function and type signature to `io`.
304305
* Specify verbosity of code comments by setting `debuginfo` to `:source` (default) or `:none`.
305306
* If `binary` is `true`, also print the binary machine code for each instruction precedented by an abbreviated address.
306307
* If `dump_module` is `false`, do not print metadata such as rodata or directives.
307-
* If `raw` is `false`, unintesting instructions (like the safepoint function prologue) are elided.
308+
* If `raw` is `false`, uninteresting instructions (like the safepoint function prologue) are elided.
308309
309310
See also: [`@code_native`](@ref), [`code_llvm`](@ref), [`code_typed`](@ref) and [`code_lowered`](@ref)
310311
"""
311312
function code_native(io::IO, @nospecialize(f), @nospecialize(types=Base.default_tt(f));
312313
dump_module::Bool=true, syntax::Symbol=:intel, raw::Bool=false,
313314
debuginfo::Symbol=:default, binary::Bool=false)
314-
d = _dump_function(f, types, true, false, false, dump_module, syntax, true, debuginfo, binary, raw)
315+
d = _dump_function(f, types, true, false, raw, dump_module, syntax, true, debuginfo, binary)
315316
if highlighting[:native] && get(io, :color, false)::Bool
316317
print_native(io, d)
317318
else

test/compiler/codegen.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end
1717

1818
# The tests below assume a certain format and safepoint_on_entry=true breaks that.
1919
function get_llvm(@nospecialize(f), @nospecialize(t), raw=true, dump_module=false, optimize=true)
20-
d = InteractiveUtils._dump_function(f, t, false, false, !raw, dump_module, :att, optimize, :none, false, false)
20+
d = InteractiveUtils._dump_function(f, t, false, false, raw, dump_module, :att, optimize, :none, false)
2121
sprint(print, d)
2222
end
2323

test/reflection.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,9 @@ _test_at_locals2(1,1,0.5f0)
909909
f31687_parent() = f31687_child(0)
910910
params = Base.CodegenParams()
911911
_dump_function(f31687_parent, Tuple{},
912-
#=native=#false, #=wrapper=#false, #=strip=#false,
912+
#=native=#false, #=wrapper=#false, #=raw=#true,
913913
#=dump_module=#true, #=syntax=#:att, #=optimize=#false, :none,
914-
#=binary=#false, #=safepoint=#false)
914+
#=binary=#false)
915915
end
916916

917917
@test nameof(Any) === :Any

0 commit comments

Comments
 (0)