implements PR-O4.2

This commit is contained in:
2026-03-24 13:42:28 +00:00
parent d4f6d96437
commit 3449771f33
8 changed files with 160 additions and 17 deletions
@@ -4,6 +4,8 @@ import lombok.Builder;
import lombok.Getter;
import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.compiler.source.tables.CallableTable;
import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.compiler.source.tables.IntrinsicTable;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.ArrayList;
@@ -19,6 +21,8 @@ public class IRBackend {
@Builder.Default
private final ReadOnlyList<CallableSignatureRef> callableSignatures = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<IntrinsicReference> intrinsicPool = ReadOnlyList.empty();
@Builder.Default
private final IRReservedMetadata reservedMetadata = IRReservedMetadata.empty();
public static IRBackendAggregator aggregator() {
@@ -29,6 +33,7 @@ public class IRBackend {
private final ArrayList<IRFunction> functions = new ArrayList<>();
private final ArrayList<IRBackendExecutableFunction> executableFunctions = new ArrayList<>();
private final CallableTable callableTable = new CallableTable();
private final IntrinsicTable intrinsicTable = new IntrinsicTable();
private final ArrayList<IRReservedMetadata.HostMethodBinding> hostMethodBindings = new ArrayList<>();
private final ArrayList<IRReservedMetadata.BuiltinTypeSurface> builtinTypeSurfaces = new ArrayList<>();
private final ArrayList<IRReservedMetadata.BuiltinConstSurface> builtinConstSurfaces = new ArrayList<>();
@@ -40,8 +45,9 @@ public class IRBackend {
}
functions.addAll(backendFile.functions().asList());
final var callableRemap = reindexCallables(backendFile.callableSignatures());
final var intrinsicRemap = reindexIntrinsics(backendFile.intrinsicPool());
for (final var function : backendFile.executableFunctions()) {
executableFunctions.add(remapExecutableFunction(function, callableRemap));
executableFunctions.add(remapExecutableFunction(function, callableRemap, intrinsicRemap));
}
final var metadata = backendFile.reservedMetadata();
hostMethodBindings.addAll(metadata.hostMethodBindings().asList());
@@ -63,7 +69,8 @@ public class IRBackend {
private IRBackendExecutableFunction remapExecutableFunction(
final IRBackendExecutableFunction function,
final int[] callableRemap) {
final int[] callableRemap,
final int[] intrinsicRemap) {
final var remappedInstructions = new ArrayList<IRBackendExecutableFunction.Instruction>(function.instructions().size());
for (final var instruction : function.instructions()) {
final Integer remappedCallee;
@@ -72,13 +79,23 @@ public class IRBackend {
} else {
remappedCallee = remapCallableId(instruction.calleeCallableId(), callableRemap, "callee");
}
final IRBackendExecutableFunction.IntrinsicCallMetadata remappedIntrinsic;
if (instruction.kind() == IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC && instruction.intrinsicCall() != null) {
final var intrinsic = instruction.intrinsicCall();
remappedIntrinsic = new IRBackendExecutableFunction.IntrinsicCallMetadata(
intrinsic.canonicalName(),
intrinsic.canonicalVersion(),
remapIntrinsicId(intrinsic.intrinsicId(), intrinsicRemap));
} else {
remappedIntrinsic = instruction.intrinsicCall();
}
remappedInstructions.add(new IRBackendExecutableFunction.Instruction(
instruction.kind(),
instruction.calleeModuleKey(),
instruction.calleeCallableName(),
remappedCallee,
instruction.hostCall(),
instruction.intrinsicCall(),
remappedIntrinsic,
instruction.label(),
instruction.targetLabel(),
instruction.expectedArgSlots(),
@@ -110,6 +127,26 @@ public class IRBackend {
return callableRemap[localCallableId];
}
private int[] reindexIntrinsics(final ReadOnlyList<IntrinsicReference> localIntrinsicPool) {
if (localIntrinsicPool == null || localIntrinsicPool.isEmpty()) {
return new int[0];
}
final var remap = new int[localIntrinsicPool.size()];
for (var i = 0; i < localIntrinsicPool.size(); i++) {
remap[i] = intrinsicTable.register(localIntrinsicPool.get(i)).getId();
}
return remap;
}
private int remapIntrinsicId(
final int localIntrinsicId,
final int[] intrinsicRemap) {
if (localIntrinsicId < 0 || localIntrinsicId >= intrinsicRemap.length) {
throw new IllegalArgumentException("invalid local intrinsic id: " + localIntrinsicId);
}
return intrinsicRemap[localIntrinsicId];
}
private ReadOnlyList<CallableSignatureRef> emitCallableSignatures() {
final var signatures = new ArrayList<CallableSignatureRef>(callableTable.size());
for (final var callableId : callableTable.identifiers()) {
@@ -118,12 +155,21 @@ public class IRBackend {
return ReadOnlyList.wrap(signatures);
}
private ReadOnlyList<IntrinsicReference> emitIntrinsicPool() {
final var pool = new ArrayList<IntrinsicReference>(intrinsicTable.size());
for (final var intrinsicId : intrinsicTable.identifiers()) {
pool.add(intrinsicTable.get(intrinsicId));
}
return ReadOnlyList.wrap(pool);
}
public IRBackend emit() {
return IRBackend
.builder()
.functions(ReadOnlyList.wrap(functions))
.executableFunctions(ReadOnlyList.wrap(executableFunctions))
.callableSignatures(emitCallableSignatures())
.intrinsicPool(emitIntrinsicPool())
.reservedMetadata(new IRReservedMetadata(
ReadOnlyList.wrap(hostMethodBindings),
ReadOnlyList.wrap(builtinTypeSurfaces),
@@ -139,6 +185,7 @@ public class IRBackend {
sb.append("IRBackend{functions=").append(functions.size())
.append(", executableFunctions=").append(executableFunctions.size())
.append(", callableSignatures=").append(callableSignatures.size())
.append(", intrinsicPool=").append(intrinsicPool.size())
.append(", hostBindings=").append(reservedMetadata.hostMethodBindings().size())
.append(", builtinTypes=").append(reservedMetadata.builtinTypeSurfaces().size())
.append(", builtinConsts=").append(reservedMetadata.builtinConstSurfaces().size())
@@ -2,6 +2,7 @@ package p.studio.compiler.models;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.Objects;
@@ -11,29 +12,31 @@ public record IRBackendFile(
ReadOnlyList<IRFunction> functions,
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
IRReservedMetadata reservedMetadata,
ReadOnlyList<CallableSignatureRef> callableSignatures) {
ReadOnlyList<CallableSignatureRef> callableSignatures,
ReadOnlyList<IntrinsicReference> intrinsicPool) {
public IRBackendFile {
fileId = Objects.requireNonNull(fileId, "fileId");
functions = functions == null ? ReadOnlyList.empty() : functions;
executableFunctions = executableFunctions == null ? ReadOnlyList.empty() : executableFunctions;
reservedMetadata = reservedMetadata == null ? IRReservedMetadata.empty() : reservedMetadata;
callableSignatures = callableSignatures == null ? ReadOnlyList.empty() : callableSignatures;
intrinsicPool = intrinsicPool == null ? ReadOnlyList.empty() : intrinsicPool;
}
public IRBackendFile(
final FileId fileId,
final ReadOnlyList<IRFunction> functions) {
this(fileId, functions, ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty());
this(fileId, functions, ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty(), ReadOnlyList.empty());
}
public static IRBackendFile empty(final FileId fileId) {
return new IRBackendFile(fileId, ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty());
return new IRBackendFile(fileId, ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty(), ReadOnlyList.empty());
}
public IRBackendFile(
final FileId fileId,
final ReadOnlyList<IRFunction> functions,
final IRReservedMetadata reservedMetadata) {
this(fileId, functions, ReadOnlyList.empty(), reservedMetadata, ReadOnlyList.empty());
this(fileId, functions, ReadOnlyList.empty(), reservedMetadata, ReadOnlyList.empty(), ReadOnlyList.empty());
}
}