Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,186 @@
|
||||
#! @file merge.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the merging of the stores filling 16 bytes of a cell into one, of a
|
||||
#! vector built in a register, which an executable's code is given once it is
|
||||
#! optimized. A load reading several stores waits until each reaches the cache,
|
||||
#! where one store of all its bytes is forwarded to it at once, and a struct of
|
||||
#! four `f32`s or two words, as a `vec3` holding its fourth, is copied out 16
|
||||
#! bytes at a time, as it is returned: built a field at a time, the copy waited
|
||||
#! on every field.
|
||||
#!
|
||||
#! The 16 bytes from a multiple of 16 in a cell the frame zeroes are merged
|
||||
#! where every instruction writing them in the code is a store of 4 or 8 bytes,
|
||||
#! all reals or all words, each in one block, and nothing reads them from the
|
||||
#! first to the last; what none writes is zero. The cell's address goes only to
|
||||
#! what reads, writes, copies, clears, and returns it. It is done once nothing
|
||||
#! more is inlined, so a function copying the code holds its fields as values
|
||||
#! still, as promoting holds them, and only in an executable's code, which holds
|
||||
#! vectors.
|
||||
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What merging knows of a write or read of a cell.
|
||||
struct touch {
|
||||
var at: u32; #! The instruction.
|
||||
var offset: u64; #! The first byte.
|
||||
var size: u64; #! How many bytes.
|
||||
var writes: bool; #! Whether it writes them.
|
||||
var how: access; #! For a store, how it writes them; `access::bytes` for any other.
|
||||
}
|
||||
|
||||
#! Merges the stores filling 16 bytes of a cell, as the file says.
|
||||
#! @param this The code.
|
||||
#! @returns Whether anything changed.
|
||||
pub def code::merge_stores(&mut this): bool {
|
||||
return false if !this.vectors || this.cells.length == 0;
|
||||
val addresses := this.cell_table();
|
||||
return false if !addresses.any;
|
||||
|
||||
# What each cell's address goes to, where it goes only to what reads and
|
||||
# writes it, and the order of each instruction within its block.
|
||||
var touches := vector::<vector<touch>>::new(scratch());
|
||||
touches.resize(this.cells.length);
|
||||
var whole := vector::<bool>::new(scratch());
|
||||
whole.resize(this.cells.length, true);
|
||||
var place := vector::<u32>::new(scratch());
|
||||
place.resize(this.insts.length);
|
||||
for b in 0..this.blocks.length {
|
||||
continue if this.blocks[b].gone;
|
||||
var number: u32 = 0;
|
||||
var walk := this.walk(b as u32);
|
||||
while walk.more() {
|
||||
place[walk.next()] = number;
|
||||
number++;
|
||||
}
|
||||
}
|
||||
var walk := this.walk_all();
|
||||
var reads := this.reader();
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.insts[at];
|
||||
reads.start(at);
|
||||
while reads.more() {
|
||||
val cell := addresses.cell(reads.next());
|
||||
continue if cell == 0;
|
||||
val c := (cell - 1) as u64;
|
||||
val i := reads.operand();
|
||||
if i == no_operand {
|
||||
whole[c] = false;
|
||||
} else if held.kind == op::load && i == 0 {
|
||||
touches[c].append(touch { at = at, offset = held.imm, size = (held.aux as access).width(), how = access::bytes });
|
||||
} else if held.kind == op::store && i == 0 {
|
||||
val how := held.aux as access;
|
||||
touches[c].append(touch { at = at, offset = held.imm, size = how.width(), writes = true, how = how });
|
||||
} else if held.kind == op::clear && i == 0 {
|
||||
touches[c].append(touch { at = at, size = held.imm, writes = true, how = access::bytes });
|
||||
} else if held.kind == op::copy && i < 2 {
|
||||
touches[c].append(touch { at = at, size = held.imm, writes = i == 0, how = access::bytes });
|
||||
} else if held.kind == op::ret {
|
||||
touches[c].append(touch { at = at, size = this.cells[c].size as u64, how = access::bytes });
|
||||
} else {
|
||||
whole[c] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var done := rewrite::new();
|
||||
var zeros := zero_values {};
|
||||
for c in 0..this.cells.length {
|
||||
val held: cell = this.cells[c];
|
||||
continue if !whole[c] || (held.kind != ck::temporary && held.kind != ck::local && held.kind != ck::environment);
|
||||
var start: u64 = 0;
|
||||
while start + 16 <= held.size as u64 {
|
||||
this.merge_region(c as u32, start, touches[c].span, place.span, &mut zeros, &mut done);
|
||||
start += 16;
|
||||
}
|
||||
}
|
||||
this.apply(&done);
|
||||
return done.changed;
|
||||
}
|
||||
|
||||
#! Merges the stores filling the 16 bytes of a cell from an offset, where they
|
||||
#! may be merged, as the file says: the last becomes the store of the vector
|
||||
#! of them all, built before it, and the rest go.
|
||||
#! @param this The code.
|
||||
#! @param cell The cell.
|
||||
#! @param start Where the bytes begin.
|
||||
#! @param touches What reads and writes the cell.
|
||||
#! @param place The order of each instruction within its block.
|
||||
#! @param zeros The zero of each type, made once.
|
||||
#! @param done What the pass changes.
|
||||
def code::merge_region(&mut this, cell: u32, start: u64, touches: span<touch>, place: span<u32>, zeros: &mut zero_values, done: &mut rewrite): void {
|
||||
val end := start + 16;
|
||||
var stores := vector::<touch>::new(scratch());
|
||||
var block: u32 = 0;
|
||||
for &each in touches {
|
||||
continue if !each.writes || each.offset >= end || each.offset + each.size <= start;
|
||||
return if each.how == access::bytes || each.offset < start || each.offset + each.size > end;
|
||||
val one: inst = this.insts[each.at];
|
||||
if stores.length == 0 {
|
||||
block = one.block;
|
||||
}
|
||||
# One block's stores, each of a word or a real of the same width, at an
|
||||
# offset of its own, as a struct's fields are written.
|
||||
return if one.block != block || each.size != stores_width(&stores, each.size) || (each.size != 4 && each.size != 8);
|
||||
return if stores.length != 0 && (each.how.value_type() != stores[0].how.value_type());
|
||||
for &other in stores {
|
||||
return if other.offset == each.offset;
|
||||
}
|
||||
stores.append(each);
|
||||
}
|
||||
return if stores.length < 2;
|
||||
var first: u32 = 0xFFFFFFFF;
|
||||
var last: u32 = 0;
|
||||
var last_at: u32 = 0;
|
||||
for &each in stores {
|
||||
val p := place[each.at];
|
||||
first = first < p ? first : p;
|
||||
if p >= last {
|
||||
last = p;
|
||||
last_at = each.at;
|
||||
}
|
||||
}
|
||||
# Nothing reads the bytes between the first store and the last.
|
||||
for &each in touches {
|
||||
continue if each.writes || each.offset >= end || each.offset + each.size <= start;
|
||||
val p := place[each.at];
|
||||
return if this.insts[each.at].block == block && p > first && p < last;
|
||||
}
|
||||
|
||||
val width := stores[0].size;
|
||||
val real := stores[0].how.value_type() == vt::real;
|
||||
val lane: u32 = real ? (width == 4 ? lane_f32 : lane_f64) : (width == 4 ? lane_u32 : lane_u64);
|
||||
val count := 16 / width;
|
||||
var lanes := vector::<u32>::new(scratch());
|
||||
lanes.resize(count, 0);
|
||||
for &each in stores {
|
||||
lanes[(each.offset - start) / width] = this.arg(each.at, 1);
|
||||
}
|
||||
for k in 0..count {
|
||||
if lanes[k] == 0 {
|
||||
lanes[k] = zeros.of(this, real ? vt::real : vt::word);
|
||||
}
|
||||
}
|
||||
val held: inst = this.insts[last_at];
|
||||
var gives: vt[1] = [vt::v128];
|
||||
val pack := this.detached(op::lanes, lanes.span, gives[0:1], lanes_pack, lane as u64 | 16 << 8, held.at, held.site);
|
||||
this.link_before(pack, last_at);
|
||||
var args: u32[2] = [this.arg(last_at, 0), this.insts[pack].result];
|
||||
this.become(last_at, op::store, args[0:2], access::v128 as u32, start);
|
||||
for &each in stores {
|
||||
if each.at != last_at {
|
||||
done.remove(each.at);
|
||||
}
|
||||
}
|
||||
done.changed = true;
|
||||
}
|
||||
|
||||
# The width the stores of a region share: that of the first, or the one given
|
||||
# where there is none yet.
|
||||
def stores_width(stores: &vector<touch>, size: u64): u64 {
|
||||
return stores.length == 0 ? size : stores[0].size;
|
||||
}
|
||||
Reference in new issue
Block a user