[abi] AsmJit v1.18 - performance and memory footprint improvements

* Refactored the whole codebase to use snake_case convention to
    name functions and variables, including member variables.
    Class naming is unchanged and each starts with upper-case
    character. The intention of this change is to make the source
    code more readable and consistent across multiple projects
    where AsmJit is currently used.

  * Refactored support.h to make it more shareable across projects.

  * x86::Vec now inherits from UniVec

  * minor changes in JitAllocator and WriteScope in order to make
    the size of WriteScope smaller

  * added ZoneStatistics and Zone::statistics() getter

  * improved x86::EmitHelper to use tables instead of choose() and
    other mechanisms to pick between SSE and AVX instructions

  * Refactored the whole codebase to use snake_case convention for
    for functions names, function parameter names, struct members,
    and variables

  * Added a non-owning asmjit::Span<T> type and use into public API
    to hide the usage of ZoneVector in CodeHolder, Builder, and
    Compiler. Users now only get Span (with data and size), which
    doesn't require users to know about ZoneVector

  * Removed RAWorkId from RATiedReg in favor of RAWorkReg*

  * Removed GEN from LiveInfo as it's not needed by CFG construction
    to save memory (GEN was merged with LIVE-IN bits). The remaining
    LIVE-IN, LIVE-OUT, and KILL bits are enough, however KILL bits may
    be removed in the future as KILL bits are not needed after LIVE-IN
    and LIVE-OUT converged

  * Optimized the representation of LIVE-IN, LIVE-OUT, and KILL bits
    per block. Now only registers that live across multiple basic
    blocks are included here, which means that virtual registers that
    only live in a single block are not included and won't be overhead
    during liveness analysis. This optimization alone can make liveness
    analysis 90% faster depending on the code generated (more virtual
    registers that only live in a single basic block -> more gains)

  * Optimized building liveness information bits per block. The new
    code uses an optimized algorithm to prevent too many traversals
    and uses a more optimized code for a case in which not too many
    registers are used (it avoids array operations if the number of
    all virtual registers within the function fits a single BitWord)

  * Optimized code that computes which virtual register is only used
    in a single basic block - this aims to optimize register allocator
    in the future by using a designed code path for allocating regs
    only used in a single basic block

  * Reduced the information required for each live-span, which is used
    by bin-packing. Now the struct is 8 bytes, which is good for a lot
    of optimizations C++ compiler can do

  * Added UniCompiler (ujit) which can be used to share code paths
    between X86, X86_64, and AArch64 code generation (experimental).
This commit is contained in:
kobalicek
2025-09-06 13:43:15 +02:00
parent a3199e8857
commit 7596c6d035
211 changed files with 56385 additions and 33857 deletions

View File

@@ -3,8 +3,6 @@
const fs = require("fs");
const path = require("path");
const hasOwn = Object.prototype.hasOwnProperty;
// ============================================================================
// [Tokenizer]
// ============================================================================
@@ -82,7 +80,6 @@ class Tokenizer {
function parseEnum(input) {
const map = Object.create(null);
const hasOwn = Object.prototype.hasOwnProperty;
const tokenizer = new Tokenizer(input, tokenizerPatterns);
var value = -1;
@@ -105,7 +102,7 @@ function parseEnum(input) {
value++;
}
if (!hasOwn.call(map, symbol))
if (!Object.hasOwn(map, symbol))
map[symbol] = value;
else
console.log(`${symbol} already defined, skipping...`);
@@ -173,7 +170,7 @@ function stringifyEnum(map, options) {
if (name.startsWith(stripPrefix))
name = name.substring(stripPrefix.length);
else
throw Error(`Cannot strip prefix '${stripPrefix}' in '${key}'`);
throw Error(`Cannot strip prefix '${stripPrefix}' in '${k}'`);
}
table.push({ name: name, value: map[k] });
@@ -231,8 +228,8 @@ function stringifyEnum(map, options) {
return s;
}
output += `static const char ${outputPrefix}String[] =\n` + buildStringData() + `\n`;
output += `static const ${indexType} ${outputPrefix}Index[] = {\n` + buildIndexData() + `};\n`;
output += `static const char ${outputPrefix}_data[] =\n` + buildStringData() + `\n`;
output += `static const ${indexType} ${outputPrefix}_index[] = {\n` + buildIndexData() + `};\n`;
return output;
}
@@ -336,7 +333,7 @@ class Generator {
if (!enumName)
throw Error(`Missing 'enum' in '${def[0]}`);
if (hasOwn.call(this.enumMap, enumName))
if (Object.hasOwn(this.enumMap, enumName))
throw new Error(`Enumeration '${enumName}' is already defined`);
const startIndex = src.lastIndexOf("\n", def.index) + 1;
@@ -378,7 +375,7 @@ class Generator {
if (!enumName)
throwError(`Missing 'name' in '${def[0]}`);
if (!hasOwn.call(this.enumMap, enumName))
if (!Object.hasOwn(this.enumMap, enumName))
throw new Error(`Enumeration '${enumName}' not found`);
console.log(` Injecting Enum: ${enumName}`);

View File

@@ -3,7 +3,6 @@
// See <asmjit/core.h> or LICENSE.md for license and copyright information
// SPDX-License-Identifier: Zlib
const hasOwn = Object.prototype.hasOwnProperty;
function nop(x) { return x; }
// Generator - Constants
@@ -93,11 +92,11 @@ class ObjectUtils {
return a === b;
for (let k in a)
if (!hasOwn.call(b, k) || !ObjectUtils.equals(a[k], b[k]))
if (!Object.hasOwn(b, k) || !ObjectUtils.equals(a[k], b[k]))
return false;
for (let k in b)
if (!hasOwn.call(a, k))
if (!Object.hasOwn(a, k))
return false;
}
@@ -112,11 +111,11 @@ class ObjectUtils {
return ObjectUtils.equals(a, b);
for (let k in a)
if (!hasOwn.call(except, k) && (!hasOwn.call(b, k) || !ObjectUtils.equals(a[k], b[k])))
if (!Object.hasOwn(except, k) && (!Object.hasOwn(b, k) || !ObjectUtils.equals(a[k], b[k])))
return false;
for (let k in b)
if (!hasOwn.call(except, k) && !hasOwn.call(a, k))
if (!Object.hasOwn(except, k) && !Object.hasOwn(a, k))
return false;
return true;
@@ -124,14 +123,14 @@ class ObjectUtils {
static findKey(map, keys) {
for (let key in keys)
if (hasOwn.call(map, key))
if (Object.hasOwn(map, key))
return key;
return undefined;
}
static hasAny(map, keys) {
for (let key in keys)
if (hasOwn.call(map, key))
if (Object.hasOwn(map, key))
return true;
return false;
}
@@ -139,15 +138,15 @@ class ObjectUtils {
static and(a, b) {
const out = Object.create(null);
for (let k in a)
if (hasOwn.call(b, k))
if (Object.hasOwn(b, k))
out[k] = true;
return out;
}
static xor(a, b) {
const out = Object.create(null);
for (let k in a) if (!hasOwn.call(b, k)) out[k] = true;
for (let k in b) if (!hasOwn.call(a, k)) out[k] = true;
for (let k in a) if (!Object.hasOwn(b, k)) out[k] = true;
for (let k in b) if (!Object.hasOwn(a, k)) out[k] = true;
return out;
}
}
@@ -424,8 +423,8 @@ class StringUtils {
priorityArray.forEach((str, index) => { map[str] = index; });
return function(a, b) {
const ax = hasOwn.call(map, a) ? map[a] : Infinity;
const bx = hasOwn.call(map, b) ? map[b] : Infinity;
const ax = Object.hasOwn(map, a) ? map[a] : Infinity;
const bx = Object.hasOwn(map, b) ? map[b] : Infinity;
return ax != bx ? ax - bx : a < b ? -1 : a > b ? 1 : 0;
}
}
@@ -484,7 +483,7 @@ exports.IndexedArray = IndexedArray;
// a) static const char* const* instNames = { "add", "mov", "vpunpcklbw" };
//
// b) static const char instNames[] = { "add\0" "mov\0" "vpunpcklbw\0" };
// static const uint16_t instNameIndex[] = { 0, 4, 8 };
// static const uint16_t _inst_name_index[] = { 0, 4, 8 };
//
// The latter (b) has an advantage that it doesn't have to be relocated by the linker, which saves
// a lot of space in the resulting binary and a lot of CPU cycles (and memory) when the linker loads
@@ -516,8 +515,8 @@ class IndexedString {
}
else {
for (i = 0, len = k.length; i < len; i++) {
kp = k.substr(i);
if (!hasOwn.call(partialMap, kp) || partialMap[kp].length < len)
kp = k.substring(i);
if (!Object.hasOwn(partialMap, kp) || partialMap[kp].length < len)
partialMap[kp] = k;
}
}
@@ -586,7 +585,7 @@ class IndexedString {
if (this.size === -1)
FATAL(`IndexedString.getIndex(): Not indexed yet, call index()`);
if (!hasOwn.call(this.map, k))
if (!Object.hasOwn(this.map, k))
FATAL(`IndexedString.getIndex(): Key '${k}' not found.`);
return this.map[k];

View File

@@ -7,7 +7,6 @@
const core = require("./tablegen.js");
const commons = require("./generator-commons.js");
const hasOwn = Object.prototype.hasOwnProperty;
const asmdb = core.asmdb;
const kIndent = commons.kIndent;
@@ -258,7 +257,7 @@ class EncodingTable extends core.Task {
const encoding = inst.encoding;
const opcodeData = inst.opcodeData.replace(/\(/g, "{ ").replace(/\)/g, " }");
if (!hasOwn.call(map, encoding))
if (!Object.hasOwn(map, encoding))
map[encoding] = [];
if (inst.opcodeData === "(_)") {

View File

@@ -22,9 +22,7 @@ const IndexedArray = commons.IndexedArray;
const ObjectUtils = commons.ObjectUtils;
const StringUtils = commons.StringUtils;
const hasOwn = Object.prototype.hasOwnProperty;
const disclaimer = StringUtils.disclaimer;
const decToHex = StringUtils.decToHex;
function readJSON(fileName) {
@@ -520,8 +518,8 @@ class X86TableGen extends core.TableGen {
aliases : aliasData,
mainOpcodeValue : -1, // Main opcode value (0.255 hex).
mainOpcodeIndex : -1, // Index to InstDB::_mainOpcodeTable.
altOpcodeIndex : -1, // Index to InstDB::_altOpcodeTable.
mainOpcodeIndex : -1, // Index to InstDB::main_opcode_table.
altOpcodeIndex : -1, // Index to InstDB::alt_opcode_table.
nameIndex : -1, // Index to InstDB::_nameData.
commonInfoIndex : -1,
additionalInfoIndex: -1,
@@ -979,11 +977,11 @@ class AltOpcodeTable extends core.Task {
// console.log(StringUtils.format(mainOpcodeTable, kIndent, true));
this.inject("MainOpcodeTable",
disclaimer(`const uint32_t InstDB::_mainOpcodeTable[] = {\n${StringUtils.format(mainOpcodeTable, kIndent, true)}\n};\n`),
disclaimer(`const uint32_t InstDB::main_opcode_table[] = {\n${StringUtils.format(mainOpcodeTable, kIndent, true)}\n};\n`),
mainOpcodeTable.length * 4);
this.inject("AltOpcodeTable",
disclaimer(`const uint32_t InstDB::_altOpcodeTable[] = {\n${StringUtils.format(altOpcodeTable, kIndent, true)}\n};\n`),
disclaimer(`const uint32_t InstDB::alt_opcode_table[] = {\n${StringUtils.format(altOpcodeTable, kIndent, true)}\n};\n`),
altOpcodeTable.length * 4);
}
}
@@ -1017,7 +1015,7 @@ function StringifyOpArray(a, map) {
let mapped = null;
if (typeof map === "function")
mapped = map(op);
else if (hasOwn.call(map, op))
else if (Object.hasOwn(map, op))
mapped = map[op];
else
FATAL(`UNHANDLED OPERAND '${op}'`);
@@ -1501,7 +1499,7 @@ class InstSignatureTable extends core.Task {
const signature = signatures[i];
const idx = iSignatureArr.length;
if (!hasOwn.call(iSignatureMap, signature.data))
if (!Object.hasOwn(iSignatureMap, signature.data))
iSignatureMap[signature.data] = [];
iSignatureMap[signature.data].push(idx);
@@ -1526,7 +1524,7 @@ class InstSignatureTable extends core.Task {
while (x < signature.length) {
const h = signature[x].toAsmJitOpData();
let index = -1;
if (!hasOwn.call(oSignatureMap, h)) {
if (!Object.hasOwn(oSignatureMap, h)) {
index = oSignatureArr.length;
oSignatureMap[h] = index;
oSignatureArr.push(h);
@@ -1568,12 +1566,12 @@ class InstSignatureTable extends core.Task {
` 0, \\\n` +
` { o0, o1, o2, o3, o4, o5 } \\\n` +
` }\n` +
StringUtils.makeCxxArrayWithComment(iSignatureArr, "const InstDB::InstSignature InstDB::_instSignatureTable[]") +
StringUtils.makeCxxArrayWithComment(iSignatureArr, "const InstDB::InstSignature InstDB::_inst_signature_table[]") +
`#undef ROW\n` +
`\n` +
`#define ROW(opFlags, regId) { opFlags, uint8_t(regId) }\n` +
`#define ROW(op_flags, reg_id) { op_flags, uint8_t(reg_id) }\n` +
`#define F(VAL) uint64_t(InstDB::OpFlags::k##VAL)\n` +
StringUtils.makeCxxArray(oSignatureArr, "const InstDB::OpSignature InstDB::_opSignatureTable[]") +
StringUtils.makeCxxArray(oSignatureArr, "const InstDB::OpSignature InstDB::_op_signature_table[]") +
`#undef F\n` +
`#undef ROW\n`;
this.inject("InstSignatureTable", disclaimer(s), oSignatureArr.length * 8 + iSignatureArr.length * 8);
@@ -1827,15 +1825,15 @@ class AdditionalInfoTable extends core.Task {
});
let s = `#define EXT(VAL) uint32_t(CpuFeatures::X86::k##VAL)\n` +
`const InstDB::AdditionalInfo InstDB::_additionalInfoTable[] = {\n${StringUtils.format(additionaInfoTable, kIndent, true)}\n};\n` +
`const InstDB::AdditionalInfo InstDB::additional_info_table[] = {\n${StringUtils.format(additionaInfoTable, kIndent, true)}\n};\n` +
`#undef EXT\n` +
`\n` +
`#define FLAG(VAL) uint32_t(CpuRWFlags::kX86_##VAL)\n` +
`const InstDB::RWFlagsInfoTable InstDB::_rwFlagsInfoTable[] = {\n${StringUtils.format(rwInfoTable, kIndent, true)}\n};\n` +
`const InstDB::RWFlagsInfoTable InstDB::rw_flags_info_table[] = {\n${StringUtils.format(rwInfoTable, kIndent, true)}\n};\n` +
`#undef FLAG\n` +
`\n` +
`#define FLAG(VAL) uint32_t(InstRWFlags::k##VAL)\n` +
`const InstRWFlags InstDB::_instFlagsTable[] = {\n${StringUtils.format(instFlagsTable, kIndent, true)}\n};\n` +
`const InstRWFlags InstDB::inst_flags_table[] = {\n${StringUtils.format(instFlagsTable, kIndent, true)}\n};\n` +
`#undef FLAG\n`;
this.inject("AdditionalInfoTable", disclaimer(s), additionaInfoTable.length * 8 + rwInfoTable.length * 8 + instFlagsTable.length * 4);
}
@@ -2078,17 +2076,17 @@ class InstRWInfoTable extends core.Task {
});
let s = "";
s += "const uint8_t InstDB::rwInfoIndexA[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexA, kIndent, -1) + "\n};\n";
s += "const uint8_t InstDB::rw_info_index_a_table[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexA, kIndent, -1) + "\n};\n";
s += "\n";
s += "const uint8_t InstDB::rwInfoIndexB[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexB, kIndent, -1) + "\n};\n";
s += "const uint8_t InstDB::rw_info_index_b_table[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexB, kIndent, -1) + "\n};\n";
s += "\n";
s += "const InstDB::RWInfo InstDB::rwInfoA[] = {\n" + StringUtils.format(this.rwInfoTableA, kIndent, true) + "\n};\n";
s += "const InstDB::RWInfo InstDB::rw_info_a_table[] = {\n" + StringUtils.format(this.rwInfoTableA, kIndent, true) + "\n};\n";
s += "\n";
s += "const InstDB::RWInfo InstDB::rwInfoB[] = {\n" + StringUtils.format(this.rwInfoTableB, kIndent, true) + "\n};\n";
s += "const InstDB::RWInfo InstDB::rw_info_b_table[] = {\n" + StringUtils.format(this.rwInfoTableB, kIndent, true) + "\n};\n";
s += "\n";
s += "const InstDB::RWInfoOp InstDB::rwInfoOp[] = {\n" + StringUtils.format(this.opInfoTable, kIndent, true) + "\n};\n";
s += "const InstDB::RWInfoOp InstDB::rw_info_op_table[] = {\n" + StringUtils.format(this.opInfoTable, kIndent, true) + "\n};\n";
s += "\n";
s += "const InstDB::RWInfoRm InstDB::rwInfoRm[] = {\n" + StringUtils.format(this.rmInfoTable, kIndent, true) + "\n};\n";
s += "const InstDB::RWInfoRm InstDB::rw_info_rm_table[] = {\n" + StringUtils.format(this.rmInfoTable, kIndent, true) + "\n};\n";
const size = this.rwInfoIndexA.length +
this.rwInfoIndexB.length +
@@ -2622,7 +2620,7 @@ class InstCommonTable extends core.Task {
`#define X(VAL) uint32_t(InstDB::Avx512Flags::k##VAL)\n` +
`#define CONTROL_FLOW(VAL) uint8_t(InstControlFlow::k##VAL)\n` +
`#define SAME_REG_HINT(VAL) uint8_t(InstSameRegHint::k##VAL)\n` +
`const InstDB::CommonInfo InstDB::_commonInfoTable[] = {\n${StringUtils.format(table, kIndent, true)}\n};\n` +
`const InstDB::CommonInfo InstDB::_inst_common_info_table[] = {\n${StringUtils.format(table, kIndent, true)}\n};\n` +
`#undef SAME_REG_HINT\n` +
`#undef CONTROL_FLOW\n` +
`#undef X\n` +

View File

@@ -25,8 +25,6 @@ const asmdb = require("../db");
exports.asmdb = asmdb;
exports.exp = asmdb.base.exp;
const hasOwn = Object.prototype.hasOwnProperty;
const FATAL = commons.FATAL;
const StringUtils = commons.StringUtils;
@@ -250,7 +248,7 @@ class InstructionNameData {
if (this.size === -1)
FATAL(`IndexedString.getIndex(): Not indexed yet, call index()`);
if (!hasOwn.call(this.map, k))
if (!Object.hasOwn(this.map, k))
FATAL(`IndexedString.getIndex(): Key '${k}' not found.`);
return this.map[k];
@@ -601,7 +599,7 @@ function generateNameData(out, instructions, generateAliases) {
}
var s = "";
s += `const InstNameIndex InstDB::instNameIndex = {{\n`;
s += `const InstNameIndex InstDB::_inst_name_index = {{\n`;
for (var i = 0; i < instFirst.length; i++) {
const firstId = instFirst[i] || none;
const lastId = instLast[i] || none;
@@ -613,19 +611,19 @@ function generateNameData(out, instructions, generateAliases) {
}
s += `}, uint16_t(${instNameData.maxNameLength})};\n`;
s += `\n`;
s += instNameData.formatStringTable("InstDB::_instNameStringTable");
s += instNameData.formatStringTable("InstDB::_inst_name_string_table");
s += `\n`;
s += instNameData.formatIndexTable("InstDB::_instNameIndexTable");
s += instNameData.formatIndexTable("InstDB::_inst_name_index_table");
let dataSize = instNameData.getSize() + 26 * 4;
if (generateAliases) {
s += `\n`;
s += aliasNameData.formatStringTable("InstDB::_aliasNameStringTable");
s += aliasNameData.formatStringTable("InstDB::alias_name_string_table");
s += `\n`;
s += aliasNameData.formatIndexTable("InstDB::_aliasNameIndexTable");
s += aliasNameData.formatIndexTable("InstDB::alias_name_index_table");
s += "\n";
s += "const uint32_t InstDB::_aliasIndexToInstId[] = {\n" + StringUtils.format(aliasLinkData, " ", true, null) + "\n};\n";
s += "const uint32_t InstDB::alias_index_to_inst_id_table[] = {\n" + StringUtils.format(aliasLinkData, " ", true, null) + "\n};\n";
dataSize += aliasNameData.getSize();
let info = `static constexpr uint32_t kAliasTableSize = ${aliasLinkData.length};\n`;