Skip to content

Commit

Permalink
Merge pull request #5614 from roc-lang/llvm-15-preparations
Browse files Browse the repository at this point in the history
Llvm 15 preparations
  • Loading branch information
folkertdev committed Jun 27, 2023
2 parents a891fbb + 1c52c23 commit 7b19a15
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion crates/compiler/build/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn gen_from_mono_module_llvm<'a>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let enum_attr = context.create_enum_attribute(kind_id, 1);
let enum_attr = context.create_enum_attribute(kind_id, 0);

for function in module.get_functions() {
let name = function.get_name().to_str().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/compiler/gen_llvm/src/llvm/bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn build_transform_caller_help<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -408,7 +408,7 @@ fn build_rc_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -497,7 +497,7 @@ pub fn build_eq_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -598,7 +598,7 @@ pub fn build_compare_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down
29 changes: 19 additions & 10 deletions crates/compiler/gen_llvm/src/llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,8 +1941,12 @@ fn tag_pointer_set_tag_id<'ctx>(

// NOTE: assumes the lower bits of `cast_pointer` are all 0
let indexed_pointer = unsafe {
env.builder
.build_in_bounds_gep(cast_pointer, &[tag_id_intval], "indexed_pointer")
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
cast_pointer,
&[tag_id_intval],
"indexed_pointer",
)
};

env.builder
Expand Down Expand Up @@ -1994,7 +1998,14 @@ pub fn tag_pointer_clear_tag_id<'ctx>(
"cast_to_i8_ptr",
);

let indexed_pointer = unsafe { env.builder.build_gep(cast_pointer, &[index], "new_ptr") };
let indexed_pointer = unsafe {
env.builder.new_build_in_bounds_gep(
env.context.i8_type(),
cast_pointer,
&[index],
"new_ptr",
)
};

env.builder
.build_pointer_cast(indexed_pointer, pointer.get_type(), "cast_from_i8_ptr")
Expand Down Expand Up @@ -3954,11 +3965,9 @@ fn expose_function_to_host_help_c_abi_gen_test<'a, 'ctx>(
arguments_for_call.push(*arg);
} else {
match layout_interner.get_repr(*layout) {
LayoutRepr::Builtin(Builtin::List(_)) => {
let list_type = arg_type
.into_pointer_type()
.get_element_type()
.into_struct_type();
repr @ LayoutRepr::Builtin(Builtin::List(_)) => {
let list_type = basic_type_from_layout(env, layout_interner, repr);

let loaded = env.builder.new_build_load(
list_type,
arg.into_pointer_value(),
Expand Down Expand Up @@ -5207,14 +5216,14 @@ fn build_proc_header<'a, 'ctx>(
if false {
let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let enum_attr = env.context.create_enum_attribute(kind_id, 1);
let enum_attr = env.context.create_enum_attribute(kind_id, 0);
fn_val.add_attribute(AttributeLoc::Function, enum_attr);
}

if false {
let kind_id = Attribute::get_named_enum_kind_id("noinline");
debug_assert!(kind_id > 0);
let enum_attr = env.context.create_enum_attribute(kind_id, 1);
let enum_attr = env.context.create_enum_attribute(kind_id, 0);
fn_val.add_attribute(AttributeLoc::Function, enum_attr);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/compiler/gen_llvm/src/llvm/lowlevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,19 +1844,19 @@ fn throw_because_overflow<'ctx>(env: &Env<'_, 'ctx, '_>, message: &str) {
// prevent inlining of this function
let kind_id = Attribute::get_named_enum_kind_id("noinline");
debug_assert!(kind_id > 0);
let enum_attr = env.context.create_enum_attribute(kind_id, 1);
let enum_attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, enum_attr);

// calling this function is unlikely
let kind_id = Attribute::get_named_enum_kind_id("cold");
debug_assert!(kind_id > 0);
let enum_attr = env.context.create_enum_attribute(kind_id, 1);
let enum_attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, enum_attr);

// this function never returns
let kind_id = Attribute::get_named_enum_kind_id("noreturn");
debug_assert!(kind_id > 0);
let enum_attr = env.context.create_enum_attribute(kind_id, 1);
let enum_attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, enum_attr);

// Add a basic block for the entry point
Expand Down
37 changes: 18 additions & 19 deletions crates/compiler/gen_llvm/src/llvm/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ impl<'ctx> RocStruct<'ctx> {
index_struct_value(env, layout_interner, field_layouts, *argument, index)
}
(Self::ByReference(ptr), LayoutRepr::Struct(field_layouts)) => {
index_struct_ptr(env, layout_interner, field_layouts, *ptr, index)
let struct_type = basic_type_from_layout(env, layout_interner, struct_layout);
index_struct_ptr(
env,
layout_interner,
struct_type.into_struct_type(),
field_layouts,
*ptr,
index,
)
}
(other, layout) => {
unreachable!(
Expand Down Expand Up @@ -135,26 +143,26 @@ fn index_struct_value<'a, 'ctx>(
fn index_struct_ptr<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>,
struct_type: StructType<'ctx>,
field_layouts: &[InLayout<'a>],
ptr: PointerValue<'ctx>,
index: u64,
) -> BasicValueEnum<'ctx> {
debug_assert!(!field_layouts.is_empty());

let field_value = get_field_from_ptr(
env,
ptr,
index as _,
env.arena
.alloc(format!("struct_field_access_record_{}", index)),
);

let field_layout = field_layouts[index as usize];
let field_repr = layout_interner.get_repr(field_layout);

let name = format!("struct_field_access_record_{}", index);
let field_value = env
.builder
.new_build_struct_gep(struct_type, ptr, index as u32, &name)
.unwrap();

load_roc_value(
env,
layout_interner,
layout_interner.get_repr(field_layout),
field_repr,
field_value,
"struct_field",
)
Expand All @@ -171,15 +179,6 @@ fn get_field_from_value<'ctx>(
.unwrap()
}

fn get_field_from_ptr<'ctx>(
env: &Env<'_, 'ctx, '_>,
ptr: PointerValue<'ctx>,
index: u32,
name: &str,
) -> PointerValue<'ctx> {
env.builder.build_struct_gep(ptr, index, name).unwrap()
}

struct BuildStruct<'ctx> {
struct_type: StructType<'ctx>,
struct_val: StructValue<'ctx>,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/test_gen/src/helpers/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn create_llvm_module<'a>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = context.create_enum_attribute(kind_id, 1);
let attr = context.create_enum_attribute(kind_id, 0);

for function in module.get_functions() {
let name = function.get_name().to_str().unwrap();
Expand Down

0 comments on commit 7b19a15

Please sign in to comment.