diff --git a/cranelift/codegen/src/opts.rs b/cranelift/codegen/src/opts.rs index 0fbd1f09d63c..43c3eab13e5f 100644 --- a/cranelift/codegen/src/opts.rs +++ b/cranelift/codegen/src/opts.rs @@ -285,6 +285,14 @@ impl<'a, 'b, 'c> generated_code::Context for IsleContext<'a, 'b, 'c> { block_call.block(&self.ctx.func.dfg.value_lists) } + fn colocated_func_ref(&mut self, func_ref: FuncRef) -> Option { + if self.ctx.func.dfg.ext_funcs[func_ref].colocated { + Some(func_ref) + } else { + None + } + } + fn just_trap_block(&mut self, block: &Block) -> Option { self.ctx .branch_to_trap_analysis diff --git a/cranelift/codegen/src/opts/remat.isle b/cranelift/codegen/src/opts/remat.isle index 00796fc3a253..c7b7a48d506e 100644 --- a/cranelift/codegen/src/opts/remat.isle +++ b/cranelift/codegen/src/opts/remat.isle @@ -29,3 +29,11 @@ (remat x)) (rule (simplify x @ (f64const _ _)) (remat x)) + +;; Rematerialize the addresses of colocated functions in each block where +;; they're used. They are computed relative to the program counter in one or +;; two instructions, and a single address used throughout a large function +;; would otherwise be live across all of it. Other function addresses may need +;; a load (from the GOT, for instance), so they are left alone. +(rule (simplify x @ (func_addr _ (colocated_func_ref _))) + (remat x)) diff --git a/cranelift/codegen/src/prelude_opt.isle b/cranelift/codegen/src/prelude_opt.isle index d8eb08cf4057..72db199c04a3 100644 --- a/cranelift/codegen/src/prelude_opt.isle +++ b/cranelift/codegen/src/prelude_opt.isle @@ -65,6 +65,15 @@ (decl pure block_call_block (BlockCall) Block) (extern constructor block_call_block block_call_block) +;; The verifier does not model what a `FuncRef` contains. +(model FuncRef (type !)) + +;; Matches a reference to a function in the same compilation unit +;; (`colocated`), whose address is computed relative to the program counter. +(spec (colocated_func_ref func_ref) (provide (= result func_ref))) +(decl colocated_func_ref (FuncRef) FuncRef) +(extern extractor colocated_func_ref colocated_func_ref) + ;; If `block` is a "just trap" block (its terminator is an unconditional `trap` ;; and all of its other instructions are pure) return the trap code of that ;; terminating `trap`. diff --git a/cranelift/codegen/src/spec/inst_specs.isle b/cranelift/codegen/src/spec/inst_specs.isle index 654839e38333..055920d29da4 100644 --- a/cranelift/codegen/src/spec/inst_specs.isle +++ b/cranelift/codegen/src/spec/inst_specs.isle @@ -375,6 +375,11 @@ (modifies clif_trap) (provide (= clif_trap (bv_is_zero! val)))) +;; The address of a function: only its width is known. +(spec (func_addr ty func_ref) + (require (= (:lanes ty) 1)) + (provide (= (:bits ty) (widthof result)))) + (spec (iconst ty arg) (require (= (:lanes ty) 1)) (provide (= arg (zero_ext 64 result)) diff --git a/cranelift/filetests/filetests/egraph/remat-func-addr.clif b/cranelift/filetests/filetests/egraph/remat-func-addr.clif new file mode 100644 index 000000000000..9f603e52804c --- /dev/null +++ b/cranelift/filetests/filetests/egraph/remat-func-addr.clif @@ -0,0 +1,33 @@ +test optimize +set opt_level=speed +target x86_64 + +;; The address of the colocated function `%g` is recomputed in `block1`. The +;; address of `%h`, which may need a load, is reused from `block0`. + +function %f(i64, i32) { + fn0 = colocated %g() + fn1 = %h() + +block0(v0: i64, v1: i32): + v2 = func_addr.i64 fn0 + v3 = func_addr.i64 fn1 + store v2, v0 + store v3, v0+8 + brif v1, block1, block2 + +block1: + v4 = func_addr.i64 fn0 + v5 = func_addr.i64 fn1 + store v4, v0+16 + store v5, v0+24 + return + +block2: + return +} + +; check: block1: +; nextln: v6 = func_addr.i64 fn0 +; nextln: store v6, v0+16 +; nextln: store.i64 v3, v0+24