284 lines
11 KiB
Plaintext
284 lines
11 KiB
Plaintext
#! @file widths.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains what promotion does second, in an executable's code: a cell whose
|
|
#! bytes are read or written whole as a vector of 4-byte lanes in one place and
|
|
#! 4 bytes at a time in another, as a struct of `f32`s built by its fields and
|
|
#! then read as lanes, lanes written and then read by field, or an array a
|
|
#! vector's lanes were put in, holds words of two widths, which promotion leaves
|
|
#! in memory; and where four writes of 4 bytes are read back by one of 16, the
|
|
#! processor waits for them to reach the cache. So a cell whose `f32`s are
|
|
#! written alone and read as a vector of 16 bytes has each read of the vector
|
|
#! made reads of its four lanes, packed into one, a 4-byte integer it writes
|
|
#! there being written as an `f32` of the same bits, where it is zero; and a
|
|
#! cell written as vectors and read 4 bytes at a time has each of those reads
|
|
#! made a read of the vector holding it, the lane taken from it. Either way the
|
|
#! cell then holds words of one width, which promote, and what stays in memory
|
|
#! is read as it was written. Vectors of 32 bytes are so only in code for
|
|
#! processors with AVX, where they may be values.
|
|
|
|
module sakura::compiler;
|
|
|
|
|
|
#! What reading a cell's vectors and lanes alike knows of a cell, its 4-byte
|
|
#! lanes a bit each by their offset over 4.
|
|
struct cell_widths {
|
|
var refused: bool; #! Whether it is not rewritten: its address goes elsewhere, a word of it is of another width, or its vectors overlap.
|
|
var starts: u64; #! The lanes where the vectors it reads or writes begin.
|
|
var wides: u64; #! Of those, where vectors of 32 bytes begin, rather than of 16.
|
|
var covered: u64; #! The lanes within its vectors.
|
|
var stored: u64; #! The lanes within the vectors it writes.
|
|
var vector_loads: bool; #! Whether it reads a vector.
|
|
var vector_stores: bool; #! Whether it writes a vector.
|
|
var single_loads: u64; #! The lanes it reads as `f32`s.
|
|
var single_stores: u64; #! The lanes it writes as `f32`s.
|
|
var word_loads: u64; #! The lanes it reads as 4-byte integers.
|
|
var word_stores: u64; #! The lanes it writes as 4-byte integers.
|
|
var other_words: u64; #! Of those, the lanes written anything but zero.
|
|
var mode: u8; #! 1 where its vectors' reads become its lanes', 2 where its lanes' reads become its vectors', or 0.
|
|
}
|
|
|
|
#! The bytes of a cell whose widths are followed, of 64 lanes.
|
|
const widths_bytes: u64 = 256;
|
|
|
|
#! The lanes from one, as many as there are.
|
|
#! @param first The first.
|
|
#! @param count How many.
|
|
#! @returns The lanes, a bit each.
|
|
def widths_run(first: u64, count: u64): u64 {
|
|
return 0 if first >= 64;
|
|
val end := first + count > 64 ? 64 : first + count;
|
|
val high := end >= 64 ? 0xFFFFFFFFFFFFFFFFu : (1u << end) - 1;
|
|
return high & ~((1u << first) - 1);
|
|
}
|
|
|
|
#! The type of lane a read or write of 4 bytes is, as `lanes` has it.
|
|
#! @param how The access.
|
|
#! @returns The type of lane, or `lane_none`.
|
|
def widths_lane(how: access): u32 {
|
|
return match how {
|
|
access::f32 => lane_f32,
|
|
access::u32 => lane_u32,
|
|
access::i32 => lane_i32,
|
|
else => lane_none
|
|
};
|
|
}
|
|
|
|
#! Notes a vector a cell reads or writes, refusing the cell where it overlaps
|
|
#! another or is of 32 bytes where code may run without AVX.
|
|
#! @param this What is known of the cell.
|
|
#! @param offset Where the vector begins.
|
|
#! @param bytes How many bytes it is: 16 or 32.
|
|
#! @param store Whether it is written.
|
|
#! @param wide Whether the code runs only where the processor has AVX.
|
|
def cell_widths::note_vector(&mut this, offset: u64, bytes: u64, store: bool, wide: bool): void {
|
|
if offset % 4 != 0 || (bytes == 32 && !wide) {
|
|
this.refused = true;
|
|
return;
|
|
}
|
|
val first := offset / 4;
|
|
val lanes := widths_run(first, bytes / 4);
|
|
val start := 1u << first;
|
|
val long := bytes == 32;
|
|
if (this.starts & start) != 0 {
|
|
if ((this.wides & start) != 0) != long {
|
|
this.refused = true;
|
|
}
|
|
} else if (this.covered & lanes) != 0 {
|
|
this.refused = true;
|
|
}
|
|
this.starts = this.starts | start;
|
|
if long {
|
|
this.wides = this.wides | start;
|
|
}
|
|
this.covered = this.covered | lanes;
|
|
if store {
|
|
this.stored = this.stored | lanes;
|
|
this.vector_stores = true;
|
|
} else {
|
|
this.vector_loads = true;
|
|
}
|
|
}
|
|
|
|
#! Reads a cell's vectors and their lanes alike, as the file says.
|
|
#! @param this The code.
|
|
#! @returns Whether anything changed.
|
|
pub def code::unite_widths(&mut this): bool {
|
|
return false if !this.vectors || this.cells.length == 0;
|
|
val addresses := this.cell_table();
|
|
return false if !addresses.any;
|
|
var cells := vector::<cell_widths>::new(scratch());
|
|
cells.resize(this.cells.length);
|
|
for c in 0..this.cells.length {
|
|
cells[c].refused = this.cells[c].size as u64 > widths_bytes;
|
|
}
|
|
|
|
var reads := this.reader();
|
|
var walk := this.walk_all();
|
|
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;
|
|
val a := reads.operand();
|
|
val known := a == 0 && (held.kind == op::load || held.kind == op::store);
|
|
if !known {
|
|
# A copy moves its words, whichever they are; anything else keeps the
|
|
# cell as it is.
|
|
if held.kind != op::copy || a == no_operand {
|
|
cells[c].refused = true;
|
|
}
|
|
continue;
|
|
}
|
|
val how := held.aux as access;
|
|
val store := held.kind == op::store;
|
|
if how == access::v128 || how == access::v256 {
|
|
cells[c].note_vector(held.imm, how == access::v256 ? 32 : 16, store, this.wide);
|
|
continue;
|
|
}
|
|
val lane := widths_lane(how);
|
|
if lane == lane_none || held.imm % 4 != 0 {
|
|
cells[c].refused = true;
|
|
continue;
|
|
}
|
|
val bit := 1u << (held.imm / 4);
|
|
if lane == lane_f32 {
|
|
if store {
|
|
cells[c].single_stores = cells[c].single_stores | bit;
|
|
} else {
|
|
cells[c].single_loads = cells[c].single_loads | bit;
|
|
}
|
|
} else if store {
|
|
cells[c].word_stores = cells[c].word_stores | bit;
|
|
if !this.zero_bits(this.arg(at, 1)) {
|
|
cells[c].other_words = cells[c].other_words | bit;
|
|
}
|
|
} else {
|
|
cells[c].word_loads = cells[c].word_loads | bit;
|
|
}
|
|
}
|
|
}
|
|
|
|
# Its vectors' reads become its lanes' where it writes lanes within vectors
|
|
# of 16 bytes it only reads, each an `f32` or zero, and reads no integer
|
|
# there; and its lanes' reads become its vectors' where it reads lanes within
|
|
# vectors it writes, but writes no lane within any vector.
|
|
var any := false;
|
|
for c in 0..this.cells.length {
|
|
val held: cell_widths = cells[c];
|
|
continue if held.refused || held.starts == 0;
|
|
val lanes_written := held.single_stores | held.word_stores;
|
|
if !held.vector_stores && held.wides == 0 && (lanes_written & held.covered) != 0
|
|
&& ((held.other_words | held.word_loads) & held.covered) == 0 {
|
|
cells[c].mode = 1;
|
|
} else if held.vector_stores && (lanes_written & held.covered) == 0
|
|
&& ((held.single_loads | held.word_loads) & held.stored) != 0 {
|
|
cells[c].mode = 2;
|
|
}
|
|
any = any || cells[c].mode != 0;
|
|
}
|
|
return false if !any;
|
|
|
|
var found := vector::<u32>::new(scratch());
|
|
var walking := this.walk_all();
|
|
while walking.more() {
|
|
val at := walking.next();
|
|
val held: inst = this.insts[at];
|
|
continue if held.kind != op::load && held.kind != op::store;
|
|
val cell := addresses.cell(this.arg(at, 0));
|
|
continue if cell == 0 || cells[cell - 1].mode == 0;
|
|
val how := held.aux as access;
|
|
val vector := how == access::v128 || how == access::v256;
|
|
val bit := !vector && held.imm % 4 == 0 && held.imm < widths_bytes ? 1u << (held.imm / 4) : 0u;
|
|
if cells[cell - 1].mode == 1 {
|
|
val zeroed := held.kind == op::store && widths_lane(how) != lane_f32 && (cells[cell - 1].covered & bit) != 0;
|
|
if (held.kind == op::load && vector) || zeroed {
|
|
found.append(at);
|
|
}
|
|
} else if held.kind == op::load && (cells[cell - 1].stored & bit) != 0 {
|
|
found.append(at);
|
|
}
|
|
}
|
|
|
|
var done := rewrite::new();
|
|
for &at in found.span {
|
|
val held: inst = this.insts[at];
|
|
if held.kind == op::store {
|
|
this.write_zero_lane(at);
|
|
} else if (held.aux as access) == access::v128 {
|
|
this.read_lanes(at, &mut done);
|
|
} else {
|
|
val cell := addresses.cell(this.arg(at, 0)) - 1;
|
|
this.read_lane(at, &cells[cell], &mut done);
|
|
}
|
|
}
|
|
this.apply(&done);
|
|
return true;
|
|
}
|
|
|
|
#! Makes a read of a vector of four `f32`s reads of its lanes, packed.
|
|
#! @param this The code.
|
|
#! @param index The `load`.
|
|
#! @param done What the pass changes.
|
|
def code::read_lanes(&mut this, index: u32, done: &mut rewrite): void {
|
|
val held: inst = this.insts[index];
|
|
var after := index;
|
|
var lanes: u32[4] = [0, 0, 0, 0];
|
|
var gives: vt[1] = [vt::real];
|
|
var from: u32[1] = [this.arg(index, 0)];
|
|
for k in 0..4u {
|
|
val load := this.detached(op::load, from[0:1], gives[0:1], access::f32 as u32, held.imm + 4 * k, held.at, held.site);
|
|
this.link_after(load, after);
|
|
after = load;
|
|
lanes[k] = this.insts[load].result;
|
|
}
|
|
var vector: vt[1] = [vt::v128];
|
|
val pack := this.detached(op::lanes, lanes[0:4], vector[0:1], lanes_pack, lane_f32 as u64 | (16u << 8), held.at, held.site);
|
|
this.link_after(pack, after);
|
|
done.replace(held.result, this.insts[pack].result);
|
|
done.remove(index);
|
|
}
|
|
|
|
#! Makes a write of a 4-byte integer of zero within a vector read whole a write
|
|
#! of the `f32` of the same bits, as the vector's other lanes are written.
|
|
#! @param this The code.
|
|
#! @param index The `store`.
|
|
def code::write_zero_lane(&mut this, index: u32): void {
|
|
val zero := this.real_value(0);
|
|
var args: u32[2] = [this.arg(index, 0), zero];
|
|
this.become(index, op::store, args[0:2], access::f32 as u32, this.insts[index].imm);
|
|
}
|
|
|
|
#! Makes a read of 4 bytes within a vector the cell writes a read of the
|
|
#! vector, the lane taken from it.
|
|
#! @param this The code.
|
|
#! @param index The `load`.
|
|
#! @param cell What is known of the cell.
|
|
#! @param done What the pass changes.
|
|
def code::read_lane(&mut this, index: u32, cell: &cell_widths, done: &mut rewrite): void {
|
|
val held: inst = this.insts[index];
|
|
# The vector holding it begins at the nearest start at or before it.
|
|
var first := held.imm / 4;
|
|
while (cell.starts & (1u << first)) == 0 {
|
|
first--;
|
|
}
|
|
val bytes: u64 = (cell.wides & (1u << first)) != 0 ? 32 : 16;
|
|
var vector: vt[1] = [bytes == 32 ? vt::v256 : vt::v128];
|
|
var from: u32[1] = [this.arg(index, 0)];
|
|
val load := this.detached(op::load, from[0:1], vector[0:1], (bytes == 32 ? access::v256 : access::v128) as u32, 4 * first, held.at, held.site);
|
|
this.link_after(load, index);
|
|
val lane := widths_lane(held.aux as access);
|
|
var gives: vt[1] = [lane == lane_f32 ? vt::real : vt::word];
|
|
var of: u32[1] = [this.insts[load].result];
|
|
val k := (held.imm / 4 - first) as u32;
|
|
val taken := this.detached(op::lanes, of[0:1], gives[0:1], lanes_lane | (k << 8), lane as u64 | (bytes << 8), held.at, held.site);
|
|
this.link_after(taken, load);
|
|
done.replace(held.result, this.insts[taken].result);
|
|
done.remove(index);
|
|
}
|