Skip to content

Commit

Permalink
Restore Heap to Stack optimizations for LLVM >= 4 (#3030)
Browse files Browse the repository at this point in the history
Resolves #2371.
  • Loading branch information
jemc authored Feb 27, 2019
1 parent 40c748e commit 213f2e6
Show file tree
Hide file tree
Showing 13 changed files with 8 additions and 317 deletions.
9 changes: 1 addition & 8 deletions Makefile-ponyc
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,15 @@ endif

ifeq ($(llvm_version),3.9.1)
else ifeq ($(llvm_version),4.0.1)
$(warning WARNING: LLVM 4 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),5.0.0)
$(warning WARNING: LLVM 5 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),5.0.1)
$(warning WARNING: LLVM 5 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),6.0.0)
$(warning WARNING: LLVM 6 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),6.0.1)
$(warning WARNING: LLVM 6 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),7.0.1)
$(warning WARNING: LLVM 7 support is experimental and may result in decreased performance or crashes)
else ifeq ($(llvm_version),7.1.0)
$(warning WARNING: LLVM 7 support is experimental and may result in decreased performance or crashes)
else
$(warning WARNING: Unsupported LLVM version: $(llvm_version))
$(warning Please use LLVM 3.9.1)
$(warning Please use LLVM 3.9.1, 4.0.1, 5.0.0, 5.0.1, 6.0.0, 6.0.1, 7.0.1, or 7.1.0)
endif

compiler_version := "$(shell $(CC) --version | sed -n 1p)"
Expand Down
2 changes: 0 additions & 2 deletions src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ static bool init_module(compile_t* c, ast_t* program, pass_opt_t* opt, bool jit)
c->frame = NULL;

c->reach = reach_new();
c->tbaa_mds = tbaa_metadatas_new();

// This gets a real value once the instance of None has been generated.
c->none_instance = NULL;
Expand Down Expand Up @@ -956,7 +955,6 @@ void codegen_cleanup(compile_t* c)
LLVMContextDispose(c->context);
LLVMDisposeTargetData(c->target_data);
LLVMDisposeTargetMachine(c->machine);
tbaa_metadatas_free(c->tbaa_mds);
genned_strings_destroy(&c->strings);
ffi_decls_destroy(&c->ffi_decls);
reach_free(c->reach);
Expand Down
8 changes: 0 additions & 8 deletions src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ DECLARE_HASHMAP(compile_locals, compile_locals_t, compile_local_t);
typedef struct ffi_decl_t ffi_decl_t;
DECLARE_HASHMAP(ffi_decls, ffi_decls_t, ffi_decl_t);

typedef struct tbaa_metadatas_t tbaa_metadatas_t;

typedef struct compile_frame_t
{
LLVMValueRef fun;
Expand All @@ -92,7 +90,6 @@ typedef struct compile_t
{
pass_opt_t* opt;
reach_t* reach;
tbaa_metadatas_t* tbaa_mds;
genned_strings_t strings;
ffi_decls_t ffi_decls;
const char* filename;
Expand Down Expand Up @@ -178,11 +175,6 @@ typedef struct compile_t
LLVMBuilderRef builder;
LLVMDIBuilderRef di;
LLVMMetadataRef di_unit;
LLVMValueRef tbaa_root;
#if LLVM_PONY < 400
LLVMValueRef tbaa_descriptor;
LLVMValueRef tbaa_descptr;
#endif
LLVMValueRef none_instance;
LLVMValueRef primitives_init;
LLVMValueRef primitives_final;
Expand Down
20 changes: 1 addition & 19 deletions src/libponyc/codegen/genbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ LLVMValueRef gen_box(compile_t* c, ast_t* type, LLVMValueRef value)

// Store the primitive in element 1.
LLVMValueRef value_ptr = LLVMBuildStructGEP(c->builder, this_ptr, 1, "");
LLVMValueRef store = LLVMBuildStore(c->builder, value, value_ptr);

const char* box_name = genname_box(t->name);
LLVMValueRef metadata = tbaa_metadata_for_box_type(c, box_name);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, store);
#else
const char id[] = "tbaa";
LLVMSetMetadata(store, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif
LLVMBuildStore(c->builder, value, value_ptr);

return this_ptr;
}
Expand All @@ -62,14 +53,5 @@ LLVMValueRef gen_unbox(compile_t* c, ast_t* type, LLVMValueRef object)

LLVMValueRef value = LLVMBuildLoad(c->builder, value_ptr, "");

const char* box_name = genname_box(t->name);
LLVMValueRef metadata = tbaa_metadata_for_box_type(c, box_name);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, value);
#else
const char id[] = "tbaa";
LLVMSetMetadata(value, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif

return gen_assign_cast(c, c_t->use_type, value, t->ast_cap);
}
9 changes: 1 addition & 8 deletions src/libponyc/codegen/gencall.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,7 @@ static void set_descriptor(compile_t* c, reach_type_t* t, LLVMValueRef value)
return;

LLVMValueRef desc_ptr = LLVMBuildStructGEP(c->builder, value, 0, "");
LLVMValueRef store = LLVMBuildStore(c->builder,
((compile_type_t*)t->c_type)->desc, desc_ptr);
#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descptr, store);
#else
const char id[] = "tbaa";
LLVMSetMetadata(store, LLVMGetMDKindID(id, sizeof(id) - 1), c->tbaa_descptr);
#endif
LLVMBuildStore(c->builder, ((compile_type_t*)t->c_type)->desc, desc_ptr);
}

// This function builds a stack of indices such that for an AST nested in an
Expand Down
43 changes: 0 additions & 43 deletions src/libponyc/codegen/gendesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ static LLVMValueRef make_unbox_function(compile_t* c, reach_type_t* t,
LLVMValueRef primitive_ptr = LLVMBuildStructGEP(c->builder, this_ptr, 1, "");
LLVMValueRef primitive = LLVMBuildLoad(c->builder, primitive_ptr, "");

const char* box_name = genname_box(t->name);
LLVMValueRef metadata = tbaa_metadata_for_box_type(c, box_name);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, primitive);
#else
const char id[] = "tbaa";
LLVMSetMetadata(primitive, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif

primitive = gen_assign_cast(c, c_t->use_type, primitive, t->ast_cap);

LLVMValueRef* args = (LLVMValueRef*)ponyint_pool_alloc_size(buf_size);
Expand Down Expand Up @@ -489,26 +480,13 @@ static LLVMValueRef desc_field(compile_t* c, LLVMValueRef desc, int index)
{
LLVMValueRef ptr = LLVMBuildStructGEP(c->builder, desc, index, "");
LLVMValueRef field = LLVMBuildLoad(c->builder, ptr, "");
#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descriptor, field);
#else
const char id[] = "tbaa";
LLVMSetMetadata(field, LLVMGetMDKindID(id, sizeof(id) - 1),
c->tbaa_descriptor);
#endif
return field;
}

LLVMValueRef gendesc_fetch(compile_t* c, LLVMValueRef object)
{
LLVMValueRef ptr = LLVMBuildStructGEP(c->builder, object, 0, "");
LLVMValueRef desc = LLVMBuildLoad(c->builder, ptr, "");
#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descptr, desc);
#else
const char id[] = "tbaa";
LLVMSetMetadata(desc, LLVMGetMDKindID(id, sizeof(id) - 1), c->tbaa_descptr);
#endif
return desc;
}

Expand Down Expand Up @@ -542,12 +520,6 @@ LLVMValueRef gendesc_vtable(compile_t* c, LLVMValueRef desc, size_t colour)

LLVMValueRef func_ptr = LLVMBuildInBoundsGEP(c->builder, vtable, gep, 2, "");
LLVMValueRef fun = LLVMBuildLoad(c->builder, func_ptr, "");
#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descriptor, fun);
#else
const char id[] = "tbaa";
LLVMSetMetadata(fun, LLVMGetMDKindID(id, sizeof(id) - 1), c->tbaa_descriptor);
#endif
return fun;
}

Expand Down Expand Up @@ -578,13 +550,6 @@ LLVMValueRef gendesc_fieldinfo(compile_t* c, LLVMValueRef desc, size_t index)
LLVMValueRef field_desc = LLVMBuildInBoundsGEP(c->builder, fields, gep, 2,
"");
LLVMValueRef field_info = LLVMBuildLoad(c->builder, field_desc, "");
#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descriptor, field_info);
#else
const char id[] = "tbaa";
LLVMSetMetadata(field_info, LLVMGetMDKindID(id, sizeof(id) - 1),
c->tbaa_descriptor);
#endif
return field_info;
}

Expand Down Expand Up @@ -669,14 +634,6 @@ LLVMValueRef gendesc_istrait(compile_t* c, LLVMValueRef desc, ast_t* type)
LLVMValueRef index = LLVMBuildInBoundsGEP(c->builder, bitmap, args, 2, "");
index = LLVMBuildLoad(c->builder, index, "");

#if PONY_LLVM >= 400
tbaa_tag(c, c->tbaa_descriptor, index);
#else
const char id[] = "tbaa";
LLVMSetMetadata(index, LLVMGetMDKindID(id, sizeof(id) - 1),
c->tbaa_descriptor);
#endif

LLVMValueRef has_trait = LLVMBuildAnd(c->builder, index, mask, "");
has_trait = LLVMBuildICmp(c->builder, LLVMIntNE, has_trait,
LLVMConstInt(c->intptr, 0, false), "");
Expand Down
4 changes: 0 additions & 4 deletions src/libponyc/codegen/genfun.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,6 @@ void genfun_param_attrs(compile_t* c, reach_type_t* t, reach_method_t* m,

case TK_VAL:
case TK_TAG:
LLVMAddAttributeAtIndex(fun, i + offset, noalias_attr);
LLVMAddAttributeAtIndex(fun, i + offset, readonly_attr);
break;

case TK_BOX:
LLVMAddAttributeAtIndex(fun, i + offset, readonly_attr);
break;
Expand Down
21 changes: 4 additions & 17 deletions src/libponyc/codegen/genoperator.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static LLVMValueRef assign_local(compile_t* c, LLVMValueRef l_value,
}

static LLVMValueRef assign_field(compile_t* c, LLVMValueRef l_value,
LLVMValueRef r_value, ast_t* p_type, ast_t* l_type, ast_t* r_type)
LLVMValueRef r_value, ast_t* l_type, ast_t* r_type)
{
reach_type_t* t = reach_type(c->reach, l_type);
pony_assert(t != NULL);
Expand All @@ -360,18 +360,7 @@ static LLVMValueRef assign_field(compile_t* c, LLVMValueRef l_value,
return NULL;

// Store to the field.
LLVMValueRef store = LLVMBuildStore(c->builder, r_value, l_value);
LLVMValueRef metadata = tbaa_metadata_for_type(c, p_type);

#if PONY_LLVM >= 400
tbaa_tag(c, metadata, result);
tbaa_tag(c, metadata, store);
#else
const char id[] = "tbaa";
unsigned md_kind = LLVMGetMDKindID(id, sizeof(id) - 1);
LLVMSetMetadata(result, md_kind, metadata);
LLVMSetMetadata(store, md_kind, metadata);
#endif
LLVMBuildStore(c->builder, r_value, l_value);

return gen_assign_cast(c, c_t->use_type, result, l_type);
}
Expand Down Expand Up @@ -467,8 +456,7 @@ static LLVMValueRef assign_rvalue(compile_t* c, ast_t* left, ast_t* r_type,
ast_t* p_type = deferred_reify(reify, ast_type(ast_child(left)), c->opt);
ast_t* l_type = deferred_reify(reify, ast_type(left), c->opt);

LLVMValueRef ret = assign_field(c, l_value, r_value, p_type, l_type,
r_type);
LLVMValueRef ret = assign_field(c, l_value, r_value, l_type, r_type);

ast_free_unattached(p_type);
ast_free_unattached(l_type);
Expand All @@ -482,8 +470,7 @@ static LLVMValueRef assign_rvalue(compile_t* c, ast_t* left, ast_t* r_type,
ast_t* p_type = deferred_reify(reify, ast_type(ast_child(left)), c->opt);
ast_t* l_type = deferred_reify(reify, ast_type(left), c->opt);

LLVMValueRef r = assign_field(c, l_value, r_value, p_type, l_type,
r_type);
LLVMValueRef r = assign_field(c, l_value, r_value, l_type, r_type);

ast_free_unattached(p_type);
ast_free_unattached(l_type);
Expand Down
8 changes: 0 additions & 8 deletions src/libponyc/codegen/genopt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,7 @@ class HeapToStack : public FunctionPass
}

case Instruction::Load:
// This is a workaround for a problem with LLVM 4 & 5 on *nix when
// hoisting loads (see #2303, #2061, #1592).
// TODO: figure out the real reason LLVM 4 and 5 produce bad code
// when hoisting stack allocated loads.
#if PONY_LLVM >= 400
// fall through
#else
break;
#endif

case Instruction::Store:
{
Expand Down
28 changes: 1 addition & 27 deletions src/libponyc/codegen/genprim.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,6 @@ static void pointer_apply(compile_t* c, void* data, token_id cap)
token_id tmp_cap = ast_id(tcap);
ast_setid(tcap, cap);

LLVMValueRef metadata = tbaa_metadata_for_type(c, t->ast);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, result);
#else
const char id[] = "tbaa";
LLVMSetMetadata(result, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif

ast_setid(tcap, tmp_cap);

result = gen_assign_cast(c, c_t_elem->use_type, result, t_elem->ast_cap);
Expand Down Expand Up @@ -258,17 +250,7 @@ static void pointer_update(compile_t* c, reach_type_t* t,

LLVMValueRef value = LLVMGetParam(c_m->func, 2);
value = gen_assign_cast(c, c_t_elem->mem_type, value, t_elem->ast_cap);
LLVMValueRef store = LLVMBuildStore(c->builder, value, loc);

LLVMValueRef metadata = tbaa_metadata_for_type(c, t->ast);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, result);
tbaa_tag(c, metadata, store);
#else
const char id[] = "tbaa";
LLVMSetMetadata(result, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
LLVMSetMetadata(store, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif
LLVMBuildStore(c->builder, value, loc);

result = gen_assign_cast(c, c_t_elem->use_type, result, t_elem->ast_cap);
LLVMBuildRet(c->builder, result);
Expand Down Expand Up @@ -371,14 +353,6 @@ static void pointer_delete(compile_t* c, reach_type_t* t,
LLVMPointerType(c_t_elem->mem_type, 0), "");
LLVMValueRef result = LLVMBuildLoad(c->builder, elem_ptr, "");

LLVMValueRef metadata = tbaa_metadata_for_type(c, t->ast);
#if PONY_LLVM >= 400
tbaa_tag(c, metadata, result);
#else
const char id[] = "tbaa";
LLVMSetMetadata(result, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif

LLVMValueRef src = LLVMBuildInBoundsGEP(c->builder, elem_ptr, &n, 1, "");
src = LLVMBuildBitCast(c->builder, src, c_t->use_type, "");
LLVMValueRef elen = LLVMBuildMul(c->builder, len, l_size, "");
Expand Down
11 changes: 0 additions & 11 deletions src/libponyc/codegen/genreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,6 @@ LLVMValueRef gen_fieldload(compile_t* c, ast_t* ast)

field = LLVMBuildLoad(c->builder, field, "");

type = deferred_reify(reify, ast_type(left), c->opt);
LLVMValueRef metadata = tbaa_metadata_for_type(c, type);
ast_free_unattached(type);

#if PONY_LLVM >= 400
tbaa_tag(c, metadata, field);
#else
const char id[] = "tbaa";
LLVMSetMetadata(field, LLVMGetMDKindID(id, sizeof(id) - 1), metadata);
#endif

return gen_assign_cast(c, c_t->use_type, field, t->ast_cap);
}

Expand Down
Loading

0 comments on commit 213f2e6

Please sign in to comment.