From 729df81898dd5f1ad7fca971943f70bbdad3c66c Mon Sep 17 00:00:00 2001 From: Patrick LaFontaine <32135464+Pat-Lafon@users.noreply.github.com> Date: Sun, 21 Jan 2024 14:38:29 -0500 Subject: [PATCH] llvm 17 --- .github/workflows/rust.yaml | 2 +- bril-rs/brillvm/Cargo.toml | 7 +- bril-rs/brillvm/src/llvm.rs | 289 ++++++++++++++++++++++-------------- 3 files changed, 179 insertions(+), 119 deletions(-) diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index e3c395660..ef92edf5f 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -91,7 +91,7 @@ jobs: uses: KyleMayes/install-llvm-action@v1 if: matrix.path == 'bril-rs/brillvm/Cargo.toml' with: - version: "16.0" + version: "17.0" - name: cargo check run: cargo check --manifest-path ${{ matrix.path }} --all-targets diff --git a/bril-rs/brillvm/Cargo.toml b/bril-rs/brillvm/Cargo.toml index 8b430580a..adf37dc98 100644 --- a/bril-rs/brillvm/Cargo.toml +++ b/bril-rs/brillvm/Cargo.toml @@ -16,14 +16,9 @@ default-run = "main" [dependencies] clap = { version = "4.4", features = ["derive"] } -inkwell = { version = "0.2", features = ["llvm16-0"] } +inkwell = { version = "0.3", features = ["llvm17-0"] } libc-print = "0.1" -# For overriding parking_lot_core to latest version from 0.9.7 -# To pull in the removal of `window-sys` which was causing duplicate dependencies -[patch.crates-io] -parking_lot_core = { git = "https://github.com/Amanieu/parking_lot" } - [dependencies.bril-rs] path = ".." features = ["float", "ssa", "memory"] diff --git a/bril-rs/brillvm/src/llvm.rs b/bril-rs/brillvm/src/llvm.rs index 62c10838b..8a35e2753 100644 --- a/bril-rs/brillvm/src/llvm.rs +++ b/bril-rs/brillvm/src/llvm.rs @@ -65,7 +65,7 @@ fn build_load<'a>( name: &str, ) -> BasicValueEnum<'a> { llvm_type_map(context, &ptr.ty, |pointee_ty| { - builder.build_load(pointee_ty, ptr.ptr, name) + builder.build_load(pointee_ty, ptr.ptr, name).unwrap() }) } @@ -80,7 +80,7 @@ impl<'a> WrappedPointer<'a> { fn new(builder: &'a Builder, context: &'a Context, name: &str, ty: &Type) -> Self { Self { ty: ty.clone(), - ptr: llvm_type_map(context, ty, |ty| builder.build_alloca(ty, name)), + ptr: llvm_type_map(context, ty, |ty| builder.build_alloca(ty, name).unwrap()), } } } @@ -147,13 +147,15 @@ fn build_op<'a, 'b>( args: &'b [String], dest: &'b String, ) { - builder.build_store( - heap.get(dest).ptr, - op(args - .iter() - .map(|n| build_load(context, builder, &heap.get(n), &fresh.fresh_var())) - .collect()), - ); + builder + .build_store( + heap.get(dest).ptr, + op(args + .iter() + .map(|n| build_load(context, builder, &heap.get(n), &fresh.fresh_var())) + .collect()), + ) + .unwrap(); } // Like `build_op` but where there is no return value @@ -204,10 +206,12 @@ fn build_instruction<'a, 'b>( value: Literal::Int(i), } => { #[allow(clippy::cast_precision_loss)] - builder.build_store( - heap.get(dest).ptr, - context.f64_type().const_float(*i as f64), - ); + builder + .build_store( + heap.get(dest).ptr, + context.f64_type().const_float(*i as f64), + ) + .unwrap(); } Instruction::Constant { dest, @@ -216,10 +220,12 @@ fn build_instruction<'a, 'b>( value: Literal::Int(i), } => { #[allow(clippy::cast_sign_loss)] - builder.build_store( - heap.get(dest).ptr, - context.i64_type().const_int(*i as u64, true), - ); + builder + .build_store( + heap.get(dest).ptr, + context.i64_type().const_int(*i as u64, true), + ) + .unwrap(); } Instruction::Constant { dest, @@ -227,10 +233,12 @@ fn build_instruction<'a, 'b>( const_type: _, value: Literal::Bool(b), } => { - builder.build_store( - heap.get(dest).ptr, - context.bool_type().const_int((*b).into(), false), - ); + builder + .build_store( + heap.get(dest).ptr, + context.bool_type().const_int((*b).into(), false), + ) + .unwrap(); } Instruction::Constant { dest, @@ -238,7 +246,9 @@ fn build_instruction<'a, 'b>( const_type: _, value: Literal::Float(f), } => { - builder.build_store(heap.get(dest).ptr, context.f64_type().const_float(*f)); + builder + .build_store(heap.get(dest).ptr, context.f64_type().const_float(*f)) + .unwrap(); } Instruction::Value { args, @@ -261,6 +271,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -288,6 +299,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -315,6 +327,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -342,6 +355,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -370,6 +384,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -398,6 +413,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -426,6 +442,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -454,6 +471,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -482,6 +500,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -505,6 +524,7 @@ fn build_instruction<'a, 'b>( |v| { builder .build_not::(v[0].try_into().unwrap(), &ret_name) + .unwrap() .into() }, args, @@ -532,6 +552,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -559,6 +580,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -595,6 +617,7 @@ fn build_instruction<'a, 'b>( .as_slice(), &ret_name, ) + .unwrap() .try_as_basic_value() .left() .unwrap() @@ -632,6 +655,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -659,6 +683,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -686,6 +711,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -713,6 +739,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -741,6 +768,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -769,6 +797,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -797,6 +826,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -825,6 +855,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -853,6 +884,7 @@ fn build_instruction<'a, 'b>( v[1].try_into().unwrap(), &ret_name, ) + .unwrap() .into() }, args, @@ -866,14 +898,16 @@ fn build_instruction<'a, 'b>( op: EffectOps::Return, } => { if args.is_empty() { - builder.build_return(None); + builder.build_return(None).unwrap(); } else { - builder.build_return(Some(&build_load( - context, - builder, - &heap.get(&args[0]), - &fresh.fresh_var(), - ))); + builder + .build_return(Some(&build_load( + context, + builder, + &heap.get(&args[0]), + &fresh.fresh_var(), + ))) + .unwrap(); } } Instruction::Effect { @@ -895,14 +929,16 @@ fn build_instruction<'a, 'b>( heap, fresh, |v| { - builder.build_call( - function, - v.iter() - .map(|val| (*val).into()) - .collect::>() - .as_slice(), - &ret_name, - ); + builder + .build_call( + function, + v.iter() + .map(|val| (*val).into()) + .collect::>() + .as_slice(), + &ret_name, + ) + .unwrap(); }, args, ); @@ -932,33 +968,40 @@ fn build_instruction<'a, 'b>( let v = build_load(context, builder, &wrapped_ptr, &fresh.fresh_var()); match wrapped_ptr.ty { Type::Int => { - builder.build_call(print_int, &[v.into()], "print_int"); + builder + .build_call(print_int, &[v.into()], "print_int") + .unwrap(); } Type::Bool => { - builder.build_call( - print_bool, - &[builder - .build_int_cast::( - v.try_into().unwrap(), - context.bool_type(), - "bool_cast", - ) - .into()], - "print_bool", - ); + builder + .build_call( + print_bool, + &[builder + .build_int_cast::( + v.try_into().unwrap(), + context.bool_type(), + "bool_cast", + ) + .unwrap() + .into()], + "print_bool", + ) + .unwrap(); } Type::Float => { - builder.build_call(print_float, &[v.into()], "print_float"); + builder + .build_call(print_float, &[v.into()], "print_float") + .unwrap(); } Type::Pointer(_) => { unreachable!() } }; if i < len - 1 { - builder.build_call(print_sep, &[], "print_sep"); + builder.build_call(print_sep, &[], "print_sep").unwrap(); } }); - builder.build_call(print_end, &[], "print_end"); + builder.build_call(print_end, &[], "print_end").unwrap(); } Instruction::Effect { args: _, @@ -966,9 +1009,11 @@ fn build_instruction<'a, 'b>( labels, op: EffectOps::Jump, } => { - builder.build_unconditional_branch(block_map_get( - context, llvm_func, block_map, &labels[0], - )); + builder + .build_unconditional_branch(block_map_get( + context, llvm_func, block_map, &labels[0], + )) + .unwrap(); } Instruction::Effect { args, @@ -984,11 +1029,9 @@ fn build_instruction<'a, 'b>( heap, fresh, |v| { - builder.build_conditional_branch( - v[0].try_into().unwrap(), - then_block, - else_block, - ); + builder + .build_conditional_branch(v[0].try_into().unwrap(), then_block, else_block) + .unwrap(); }, args, ); @@ -1007,10 +1050,12 @@ fn build_instruction<'a, 'b>( .map(|l| block_map_get(context, llvm_func, block_map, l)) .collect::>(); - let phi = builder.build_phi( - build_pointertype(context, &Type::Pointer(Box::new(op_type.clone()))), - &name, - ); + let phi = builder + .build_phi( + build_pointertype(context, &Type::Pointer(Box::new(op_type.clone()))), + &name, + ) + .unwrap(); let pointers = args.iter().map(|a| heap.get(a).ptr).collect::>(); @@ -1024,18 +1069,20 @@ fn build_instruction<'a, 'b>( .as_slice(), ); - builder.build_store( - heap.get(dest).ptr, - build_load( - context, - builder, - &WrappedPointer { - ty: op_type.clone(), - ptr: phi.as_basic_value().into_pointer_value(), - }, - &fresh.fresh_var(), - ), - ); + builder + .build_store( + heap.get(dest).ptr, + build_load( + context, + builder, + &WrappedPointer { + ty: op_type.clone(), + ptr: phi.as_basic_value().into_pointer_value(), + }, + &fresh.fresh_var(), + ), + ) + .unwrap(); } Instruction::Value { args, @@ -1079,7 +1126,11 @@ fn build_instruction<'a, 'b>( builder, heap, fresh, - |v| builder.build_load(pointee_ty, v[0].try_into().unwrap(), &name), + |v| { + builder + .build_load(pointee_ty, v[0].try_into().unwrap(), &name) + .unwrap() + }, args, dest, ); @@ -1109,6 +1160,7 @@ fn build_instruction<'a, 'b>( &[v[1].try_into().unwrap()], &name, ) + .unwrap() .into() }) }, @@ -1128,7 +1180,7 @@ fn build_instruction<'a, 'b>( heap, fresh, |v| { - builder.build_store(v[0].try_into().unwrap(), v[1]); + builder.build_store(v[0].try_into().unwrap(), v[1]).unwrap(); }, args, ); @@ -1145,7 +1197,7 @@ fn build_instruction<'a, 'b>( heap, fresh, |v| { - builder.build_free(v[0].try_into().unwrap()); + builder.build_free(v[0].try_into().unwrap()).unwrap(); }, args, ); @@ -1224,7 +1276,7 @@ pub fn create_module_from_program<'a>( llvm_func.get_param_iter().enumerate().for_each(|(i, arg)| { let Argument { name, arg_type } = &args[i]; let ptr = heap.add(&builder, context, name, arg_type).ptr; - builder.build_store(ptr, arg); + builder.build_store(ptr, arg).unwrap(); }); instrs.iter().for_each(|i| match i { @@ -1262,12 +1314,14 @@ pub fn create_module_from_program<'a>( // Check if wee need to insert a jump since all llvm blocks must be terminated if !is_terminating_instr(&last_instr) { - builder.build_unconditional_branch(block_map_get( - context, - llvm_func, - &mut block_map, - label, - )); + builder + .build_unconditional_branch(block_map_get( + context, + llvm_func, + &mut block_map, + label, + )) + .unwrap(); } // Start a new block @@ -1297,7 +1351,7 @@ pub fn create_module_from_program<'a>( // Make sure every function is terminated with a return if not already if !is_terminating_instr(&last_instr) { - builder.build_return(None); + builder.build_return(None).unwrap(); } }); @@ -1339,37 +1393,44 @@ pub fn create_module_from_program<'a>( function.get_param_iter().enumerate().for_each(|(i, _)| { let Argument { name, arg_type } = &args[i]; let ptr = heap.add(&builder, context, name, arg_type).ptr; - let arg_str = builder.build_load( - context.i8_type().ptr_type(AddressSpace::default()), - unsafe { - builder.build_in_bounds_gep( - context - .i8_type() - .ptr_type(AddressSpace::default()) - .ptr_type(AddressSpace::default()), - argv, - &[context.i64_type().const_int((i + 1) as u64, true)], - "calculate offset", - ) - }, - "load arg", - ); + let arg_str = builder + .build_load( + context.i8_type().ptr_type(AddressSpace::default()), + unsafe { + builder + .build_in_bounds_gep( + context + .i8_type() + .ptr_type(AddressSpace::default()) + .ptr_type(AddressSpace::default()), + argv, + &[context.i64_type().const_int((i + 1) as u64, true)], + "calculate offset", + ) + .unwrap() + }, + "load arg", + ) + .unwrap(); let arg = match arg_type { Type::Int => builder .build_call(parse_int, &[arg_str.into()], "parse_int") + .unwrap() .try_as_basic_value() .unwrap_left(), Type::Bool => builder .build_call(parse_bool, &[arg_str.into()], "parse_bool") + .unwrap() .try_as_basic_value() .unwrap_left(), Type::Float => builder .build_call(parse_float, &[arg_str.into()], "parse_float") + .unwrap() .try_as_basic_value() .unwrap_left(), Type::Pointer(_) => unreachable!(), }; - builder.build_store(ptr, arg); + builder.build_store(ptr, arg).unwrap(); }); build_effect_op( @@ -1378,14 +1439,16 @@ pub fn create_module_from_program<'a>( &heap, &mut fresh, |v| { - builder.build_call( - function, - v.iter() - .map(|val| (*val).into()) - .collect::>() - .as_slice(), - "call main", - ); + builder + .build_call( + function, + v.iter() + .map(|val| (*val).into()) + .collect::>() + .as_slice(), + "call main", + ) + .unwrap(); }, &args .iter() @@ -1393,7 +1456,9 @@ pub fn create_module_from_program<'a>( .collect::>(), ); } - builder.build_return(Some(&context.i32_type().const_int(0, true))); + builder + .build_return(Some(&context.i32_type().const_int(0, true))) + .unwrap(); // Return the module runtime_module