Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cranelift/codegen/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FuncRef> {
if self.ctx.func.dfg.ext_funcs[func_ref].colocated {
Some(func_ref)
} else {
None
}
}

fn just_trap_block(&mut self, block: &Block) -> Option<TrapCode> {
self.ctx
.branch_to_trap_analysis
Expand Down
8 changes: 8 additions & 0 deletions cranelift/codegen/src/opts/remat.isle
Original file line number Diff line number Diff line change
Expand Up @@ -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))
9 changes: 9 additions & 0 deletions cranelift/codegen/src/prelude_opt.isle
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/spec/inst_specs.isle
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
33 changes: 33 additions & 0 deletions cranelift/filetests/filetests/egraph/remat-func-addr.clif
Original file line number Diff line number Diff line change
@@ -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
Loading