Skip to content

Commit c263a6e

Browse files
authored
Merge pull request #19046 from JuliaLang/tb/codegen_params
RFC: CodegenParams for external language implementations
2 parents b1e5c24 + 60a950b commit c263a6e

File tree

8 files changed

+128
-29
lines changed

8 files changed

+128
-29
lines changed

base/reflection.jl

+30-5
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,31 @@ function uncompressed_ast(m::Method, s::CodeInfo)
493493
return s
494494
end
495495

496+
# this type mirrors jl_cgparams_t (documented in julia.h)
497+
immutable CodegenParams
498+
cached::Cint
499+
500+
runtime::Cint
501+
exceptions::Cint
502+
track_allocations::Cint
503+
code_coverage::Cint
504+
static_alloc::Cint
505+
dynamic_alloc::Cint
506+
507+
CodegenParams(;cached::Bool=true,
508+
runtime::Bool=true, exceptions::Bool=true,
509+
track_allocations::Bool=true, code_coverage::Bool=true,
510+
static_alloc::Bool=true, dynamic_alloc::Bool=true) =
511+
new(Cint(cached),
512+
Cint(runtime), Cint(exceptions),
513+
Cint(track_allocations), Cint(code_coverage),
514+
Cint(static_alloc), Cint(dynamic_alloc))
515+
end
516+
496517
# Printing code representations in IR and assembly
497-
function _dump_function(f::ANY, t::ANY, native::Bool, wrapper::Bool, strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol=:att)
518+
function _dump_function(f::ANY, t::ANY, native::Bool, wrapper::Bool,
519+
strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol=:att,
520+
optimize::Bool=true, params::CodegenParams=CodegenParams())
498521
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
499522
if isa(f, Core.Builtin)
500523
throw(ArgumentError("argument is not a generic function"))
@@ -509,17 +532,19 @@ function _dump_function(f::ANY, t::ANY, native::Bool, wrapper::Bool, strip_ir_me
509532
meth = func_for_method_checked(meth, tt)
510533
linfo = ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, (Any, Any, Any), meth, tt, env)
511534
# get the code for it
512-
return _dump_function(linfo, native, wrapper, strip_ir_metadata, dump_module, syntax)
535+
return _dump_function(linfo, native, wrapper, strip_ir_metadata, dump_module, syntax, optimize, params)
513536
end
514537

515-
function _dump_function(linfo::Core.MethodInstance, native::Bool, wrapper::Bool, strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol=:att)
538+
function _dump_function(linfo::Core.MethodInstance, native::Bool, wrapper::Bool,
539+
strip_ir_metadata::Bool, dump_module::Bool, syntax::Symbol=:att,
540+
optimize::Bool=true, params::CodegenParams=CodegenParams())
516541
if syntax != :att && syntax != :intel
517542
throw(ArgumentError("'syntax' must be either :intel or :att"))
518543
end
519544
if native
520-
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool), linfo, wrapper)
545+
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool, CodegenParams), linfo, wrapper, params)
521546
else
522-
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool), linfo, wrapper)
547+
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool, Bool, CodegenParams), linfo, wrapper, optimize, params)
523548
end
524549
if llvmf == C_NULL
525550
error("could not compile the specified method")

src/alloc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ STATIC_INLINE jl_value_t *jl_call_staged(jl_svec_t *sparam_vals, jl_method_insta
457457
fptr.fptr = generator->fptr;
458458
fptr.jlcall_api = generator->jlcall_api;
459459
if (__unlikely(fptr.fptr == NULL || fptr.jlcall_api == 0)) {
460-
void *F = jl_compile_linfo(generator, (jl_code_info_t*)generator->inferred).functionObject;
460+
void *F = jl_compile_linfo(generator, (jl_code_info_t*)generator->inferred, &jl_default_cgparams).functionObject;
461461
fptr = jl_generate_fptr(generator, F);
462462
}
463463
assert(jl_svec_len(generator->def->sparam_syms) == jl_svec_len(sparam_vals));

src/cgutils.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ static Value *prepare_call(Value *Callee)
2626
}
2727

2828

29+
// --- language feature checks ---
30+
31+
// branch on whether a language feature is enabled or not
32+
#define JL_FEAT_TEST(ctx, feature) ((ctx)->params->feature)
33+
34+
// require a language feature to be enabled
35+
#define JL_FEAT_REQUIRE(ctx, feature) \
36+
if (!JL_FEAT_TEST(ctx, feature)) \
37+
jl_errorf("%s for %s:%d requires the " #feature " language feature, which is disabled", \
38+
__FUNCTION__, (ctx)->file.str().c_str(), *(ctx)->line);
39+
40+
2941
// --- string constants ---
3042
static StringMap<GlobalVariable*> stringConstants;
3143
static Value *stringConstPtr(IRBuilder<> &builder, const std::string &txt)
@@ -660,6 +672,7 @@ static void error_unless(Value *cond, const std::string &msg, jl_codectx_t *ctx)
660672
static void raise_exception(Value *exc, jl_codectx_t *ctx,
661673
BasicBlock *contBB=nullptr)
662674
{
675+
JL_FEAT_REQUIRE(ctx, runtime);
663676
#if JL_LLVM_VERSION >= 30700
664677
builder.CreateCall(prepare_call(jlthrow_func), { exc });
665678
#else
@@ -1566,6 +1579,9 @@ static void emit_cpointercheck(const jl_cgval_t &x, const std::string &msg, jl_c
15661579
// allocation for known size object
15671580
static Value *emit_allocobj(jl_codectx_t *ctx, size_t static_size, Value *jt)
15681581
{
1582+
JL_FEAT_REQUIRE(ctx, dynamic_alloc);
1583+
JL_FEAT_REQUIRE(ctx, runtime);
1584+
15691585
int osize;
15701586
int offset = jl_gc_classify_pools(static_size, &osize);
15711587
Value *ptls_ptr = emit_bitcast(ctx->ptlsStates, T_pint8);

0 commit comments

Comments
 (0)