Skip to content

Commit

Permalink
Simplify static_eval, remove allocation restriction (#37209)
Browse files Browse the repository at this point in the history
* Simplify static_eval.

Remove the sparam and allow_alloc parameters, which are enabled in all
uses of static_eval (which there are way fewer than there used to be).

* Remove static_alloc codegen param.
  • Loading branch information
maleadt committed Aug 27, 2020
1 parent 05a0d54 commit 813500d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
5 changes: 2 additions & 3 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,6 @@ default_debug_info_kind() = unsafe_load(cglobal(:jl_default_debug_info_kind, Cin
struct CodegenParams
track_allocations::Cint
code_coverage::Cint
static_alloc::Cint
prefer_specsig::Cint
gnu_pubnames::Cint
debug_info_kind::Cint
Expand All @@ -974,15 +973,15 @@ struct CodegenParams
generic_context::Any

function CodegenParams(; track_allocations::Bool=true, code_coverage::Bool=true,
static_alloc::Bool=true, prefer_specsig::Bool=false,
prefer_specsig::Bool=false,
gnu_pubnames=true, debug_info_kind::Cint = default_debug_info_kind(),
module_setup=nothing, module_activation=nothing, raise_exception=nothing,
emit_function=nothing, emitted_function=nothing,
lookup::Ptr{Cvoid}=cglobal(:jl_rettype_inferred),
generic_context = nothing)
return new(
Cint(track_allocations), Cint(code_coverage),
Cint(static_alloc), Cint(prefer_specsig),
Cint(prefer_specsig),
Cint(gnu_pubnames), debug_info_kind,
module_setup, module_activation, raise_exception,
emit_function, emitted_function, lookup,
Expand Down
10 changes: 5 additions & 5 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ static void interpret_symbol_arg(jl_codectx_t &ctx, native_sym_arg_t &out, jl_va
const char *&f_name = out.f_name;
const char *&f_lib = out.f_lib;

jl_value_t *ptr = static_eval(ctx, arg, true);
jl_value_t *ptr = static_eval(ctx, arg);
if (ptr == NULL) {
jl_cgval_t arg1 = emit_expr(ctx, arg);
jl_value_t *ptr_ty = arg1.typ;
Expand Down Expand Up @@ -557,7 +557,7 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg
JL_GC_PUSH2(&rt, &sym.gcroot);

if (nargs == 2) {
rt = static_eval(ctx, args[2], true, true);
rt = static_eval(ctx, args[2]);
if (rt == NULL) {
JL_GC_POP();
jl_cgval_t argv[2];
Expand Down Expand Up @@ -629,7 +629,7 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
JL_GC_PUSH4(&ir, &rt, &at, &entry);
if (jl_is_ssavalue(ir_arg))
ir_arg = jl_arrayref((jl_array_t*)ctx.source->code, ((jl_ssavalue_t*)ir_arg)->id - 1);
ir = static_eval(ctx, ir_arg, true, true);
ir = static_eval(ctx, ir_arg);
if (!ir) {
emit_error(ctx, "error statically evaluating llvm IR argument");
return jl_cgval_t();
Expand All @@ -640,7 +640,7 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
rt = jl_tparam0(rtt);
}
if (!rt) {
rt = static_eval(ctx, args[2], true, true);
rt = static_eval(ctx, args[2]);
if (!rt) {
emit_error(ctx, "error statically evaluating llvmcall return type");
return jl_cgval_t();
Expand All @@ -652,7 +652,7 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
at = jl_tparam0(att);
}
if (!at) {
at = static_eval(ctx, args[3], true, true);
at = static_eval(ctx, args[3]);
if (!at) {
emit_error(ctx, "error statically evaluating llvmcall argument tuple");
return jl_cgval_t();
Expand Down
1 change: 0 additions & 1 deletion src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,6 @@ static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
// language features
(a->track_allocations == b->track_allocations) &&
(a->code_coverage == b->code_coverage) &&
(a->static_alloc == b->static_alloc) &&
(a->prefer_specsig == b->prefer_specsig) &&
// hooks
(a->module_setup == b->module_setup) &&
Expand Down
18 changes: 8 additions & 10 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ static int globalUnique = 0;
// --- code generation ---
extern "C" {
int jl_default_debug_info_kind = (int) DICompileUnit::DebugEmissionKind::FullDebug;
jl_cgparams_t jl_default_cgparams = {1, 1, 1, 0,
jl_cgparams_t jl_default_cgparams = {1, 1, 0,
#ifdef _OS_WINDOWS_
0,
#else
Expand Down Expand Up @@ -1993,10 +1993,10 @@ static jl_value_t *static_apply_type(jl_codectx_t &ctx, const jl_cgval_t *args,
return result;
}

// try to statically evaluate, NULL if not possible
static jl_value_t *static_eval(jl_codectx_t &ctx, jl_value_t *ex, int sparams=true, int allow_alloc=true)
// try to statically evaluate, NULL if not possible. note that this may allocate, and as
// such the resulting value should not be embedded directly in the generated code.
static jl_value_t *static_eval(jl_codectx_t &ctx, jl_value_t *ex)
{
if (!JL_FEAT_TEST(ctx, static_alloc)) allow_alloc = 0;
if (jl_is_symbol(ex)) {
jl_sym_t *sym = (jl_sym_t*)ex;
if (jl_is_const(ctx.module, sym))
Expand Down Expand Up @@ -2032,16 +2032,16 @@ static jl_value_t *static_eval(jl_codectx_t &ctx, jl_value_t *ex, int sparams=tr
if (jl_is_expr(ex)) {
jl_expr_t *e = (jl_expr_t*)ex;
if (e->head == call_sym) {
jl_value_t *f = static_eval(ctx, jl_exprarg(e, 0), sparams, allow_alloc);
jl_value_t *f = static_eval(ctx, jl_exprarg(e, 0));
if (f) {
if (jl_array_dim0(e->args) == 3 && f == jl_builtin_getfield) {
m = (jl_module_t*)static_eval(ctx, jl_exprarg(e, 1), sparams, allow_alloc);
m = (jl_module_t*)static_eval(ctx, jl_exprarg(e, 1));
// Check the tag before evaluating `s` so that a value of random
// type won't be corrupted.
if (!m || !jl_is_module(m))
return NULL;
// Assumes that the module is rooted somewhere.
s = (jl_sym_t*)static_eval(ctx, jl_exprarg(e, 2), sparams, allow_alloc);
s = (jl_sym_t*)static_eval(ctx, jl_exprarg(e, 2));
if (s && jl_is_symbol(s)) {
jl_binding_t *b = jl_get_binding(m, s);
if (b && b->constp) {
Expand All @@ -2055,13 +2055,11 @@ static jl_value_t *static_eval(jl_codectx_t &ctx, jl_value_t *ex, int sparams=tr
size_t i;
size_t n = jl_array_dim0(e->args)-1;
if (n==0 && f==jl_builtin_tuple) return (jl_value_t*)jl_emptytuple;
if (!allow_alloc)
return NULL;
jl_value_t **v;
JL_GC_PUSHARGS(v, n+1);
v[0] = f;
for (i = 0; i < n; i++) {
v[i+1] = static_eval(ctx, jl_exprarg(e, i+1), sparams, allow_alloc);
v[i+1] = static_eval(ctx, jl_exprarg(e, i+1));
if (v[i+1] == NULL) {
JL_GC_POP();
return NULL;
Expand Down
1 change: 0 additions & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,6 @@ typedef jl_value_t *(*jl_codeinstance_lookup_t)(jl_method_instance_t *mi JL_PROP
typedef struct {
int track_allocations; // can we track allocations?
int code_coverage; // can we measure coverage?
int static_alloc; // is the compiler allowed to allocate statically?
int prefer_specsig; // are specialized function signatures preferred?

// controls the emission of debug-info. mirrors the clang options
Expand Down

2 comments on commit 813500d

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

Please sign in to comment.