Files
sakurac/compiler/optimizer/versions.sakura
T
2026-11-19 04:54:17 -06:00

172 lines
7.1 KiB
Plaintext

#! @file versions.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the versions of functions for processors with x86-64-v3's
#! instructions. A function of an executable that asks while running which
#! vector instructions the processor has, as `core::simd`'s operations do, or
#! counts a word's bits, is optimized again as a version knowing it has those of
#! x86-64-v3: SSE through SSE4.2, AVX, AVX2, FMA3, `popcnt`, `lzcnt`, and BMI1's
#! `tzcnt`. There, every operation on 16 or 32 bytes of lanes is worked with
#! instructions, so the lanes written a lane at a time go, 32 bytes of lanes are
#! values held in registers, and a count of bits is one instruction; what asks
#! for more, as 64 bytes ask for AVX-512, still asks. The function itself goes
#! to its version as it begins, where the processor has them, and its version
#! calls the versions of the functions it calls.
#!
#! A function calling one whose version is small enough to inline is given a
#! version too, though it asks nothing itself, so that its version copies the
#! other's: a loop calling `fma` works it with the instruction, rather than
#! calling a function that asks the processor each time.
#!
#! A version is optimized from the function's code as lowered, kept as it
#! began optimizing, so what it calls is inlined from their versions, and
#! its 32 bytes of lanes are promoted as the values they are, where the
#! function's own code, optimized for any processor, holds them in memory a
#! half at a time.
module sakura::compiler;
#! The instruction sets of x86-64-v3, as the executable keeps them.
pub const cpu_v3: u64 = cpu_sse2 | cpu_ssse3 | cpu_sse41 | cpu_sse42 | cpu_avx | cpu_avx2 | cpu_fma | cpu_popcnt
| cpu_lzcnt | cpu_bmi1;
#! Whether code asks which instructions the processor has, or counts a word's
#! bits, which x86-64-v3 has instructions for. The code is compacted, as each
#! function is once it is optimized, so each instruction it holds is in a block,
#! and is looked at where it lies.
#! @param this The code.
#! @returns Whether it does.
def code::asks_processor(&this): bool {
for i in 1..this.insts.length {
val kind := this.insts[i].kind;
return true if kind == op::cpu || kind == op::ones || kind == op::clz || kind == op::ctz;
}
return false;
}
#! Copies code whole, sharing no memory with it.
#! @param this The code.
#! @returns The copy.
pub def code::duplicate(&this): code {
var made := code::new();
made.insts.clear();
made.origins.clear();
made.blocks.clear();
made.insts.extend(this.insts.span);
made.operands.extend(this.operands.span);
made.origins.extend(this.origins.span);
for &each in this.blocks {
made.blocks.append(block { first = each.first, last = each.last, params = ids::copy_of(&each.params), preds = ids::copy_of(&each.preds), gone = each.gone });
}
for &each in this.edges {
made.edges.append(edge { from = each.from, target = each.target, args = ids::copy_of(&each.args), case = each.case });
}
made.cells.extend(this.cells.span);
made.results.extend(this.results.span);
made.sites.extend(this.sites.span);
made.variables.extend(this.variables.span);
made.bindings.extend(this.bindings.span);
made.returned = this.returned;
made.vectors = this.vectors;
made.wide = this.wide;
return made;
}
#! Whether a function calls one with a version small enough to be inlined
#! whole, or `@inline`, which its own version would copy.
#! @param this The compiler.
#! @param index The function.
#! @returns Whether it does.
def lowerer::calls_small_version(&mut this, index: u32): bool {
val limit := this.options.inline_limit != 0 ? this.options.inline_limit : inline_limit;
val body := &this.program.functions[index].body;
for i in 1..body.insts.length {
continue if body.insts[i].kind != op::call;
val version: u32 = this.program.functions[body.insts[i].aux].version;
continue if version == 0;
val facts := this.inline_facts(version - 1);
return true if facts.fits && (facts.cost <= limit || this.program.functions[version - 1].inline);
}
return false;
}
#! Whether the program's functions are given versions: in an executable
#! optimized at all.
#! @param this The compiler.
#! @returns Whether they are.
def lowerer::versions_made(&this): bool {
return this.options.level >= 1 && !this.options.evaluating
&& this.checker.names.text(this.checker.target.backend) == "native";
}
#! Makes the version of a function optimized for x86-64-v3, where it asks which
#! instructions the processor has, and optimizes it: what the processor has is
#! known to hold those, and each call goes to the version of what it calls,
#! where there is one.
#! @param this The compiler.
#! @param index The function, optimized.
#! @param ranks Where each function is in the order functions are optimized
#! in, as `optimizing` says, which the version is added to.
#! @param rank Where the version is: after the function, and before the
#! next in the order.
pub def lowerer::make_version(&mut this, index: u32, ranks: &mut vector<u32>, rank: u32): void {
return if !this.versions_made() || this.program.functions[index].body.wide;
# One left unfinished is not compacted, and nothing reaches it, so it is
# dropped rather than given a version.
return if this.program.functions[index].unfinished;
return if !this.program.functions[index].body.asks_processor() && !this.calls_small_version(index);
# Made from its code as lowered, where that was kept, so its lanes are
# promoted as values whole rather than as the halves its optimized code holds.
var body := code{};
if this.program.functions[index].unoptimized.insts.length != 0 {
body = move(&mut this.program.functions[index].unoptimized);
} else {
body = this.program.functions[index].body.duplicate();
}
body.wide = true;
for i in 0..body.insts.length {
val kind := body.insts[i].kind;
if kind == op::cpu {
body.insts[i].imm = cpu_v3;
} else if kind == op::call {
val version: u32 = this.program.functions[body.insts[i].aux].version;
if version != 0 {
body.insts[i].aux = version - 1;
}
}
}
val original := &this.program.functions[index];
this.program.functions.append(function {
name = string::from(original.name.span, nothing {}),
decl = original.decl,
instance = original.instance,
node = original.node,
outer = original.outer,
table = original.table,
file = original.file,
offset = original.offset,
signature = original.signature,
body = move(&mut body),
compiled = bytecode::new(),
lowered = true,
order = vector::<u32>::new(),
closure = original.closure,
callback = original.callback,
inline = original.inline
});
val made := (this.program.functions.length - 1) as u32;
this.program.functions[index].version = made + 1;
ranks.append(rank);
var found := vector::<string>::new();
this.optimize_function(made, ranks, rank, &mut found);
this.program.functions[made].optimized = true;
for &each in found {
this.problems.append(string::copy_of(&each));
}
}