Skip to content

Commit d2b73b8

Browse files
author
Killian Perlin
committed
fix native image compilation
At this commit, the internal testsuite has been shown to run as fast as the current master.
1 parent 1c84913 commit d2b73b8

File tree

6 files changed

+46
-16
lines changed

6 files changed

+46
-16
lines changed

lkql_jit/language/src/main/java/com/adacore/lkql_jit/langkit_translator/passes/Hierarchy.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.adacore.libadalang.Libadalang;
99
import com.adacore.libadalang.Libadalang.AdaNode;
10+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1011
import java.util.*;
1112

1213
/**
@@ -74,6 +75,7 @@ private void become(int size, boolean[] matrix, HashMap<String, Integer> index)
7475
this.classNamesToIndex = index;
7576
}
7677

78+
@TruffleBoundary
7779
public static Hierarchy initial() {
7880
// Collect all inital classes
7981
final Class<? extends AdaNode>[] initialClasses = Libadalang.NODE_DESCRIPTION_MAP.values()
@@ -123,10 +125,12 @@ public boolean isInstance(String classX, String classY) {
123125
classNamesToIndex.get(classY)];
124126
}
125127

128+
@TruffleBoundary
126129
public void add(String classX) {
127130
addAll(Collections.singleton(classX));
128131
}
129132

133+
@TruffleBoundary
130134
public void addAll(Collection<String> classes) {
131135
final int size = this.size() + classes.size();
132136
final boolean[] matrix = new boolean[size * size];
@@ -172,10 +176,12 @@ public void addInstanceOfRelation(String classX, String classY) {
172176
}
173177
}
174178

179+
@TruffleBoundary
175180
public void remove(String classX) {
176181
removeAll(Collections.singleton(classX));
177182
}
178183

184+
@TruffleBoundary
179185
public void removeAll(Collection<String> classes) {
180186
// Compute all the subtypes of X to mask wich row/column to keep
181187
final boolean[] deleteMask = new boolean[size()];
@@ -230,6 +236,7 @@ public void removeAll(Collection<String> classes) {
230236
}
231237

232238
// Primarily used in testing
239+
@TruffleBoundary
233240
public boolean equals(Object otherObject) {
234241
if (otherObject == null) return false;
235242
if (otherObject instanceof Hierarchy other) {

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/pass/ClassDecl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.adacore.lkql_jit.nodes.pass;
77

8+
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
89
import com.adacore.lkql_jit.nodes.LKQLNode;
910
import com.oracle.truffle.api.frame.VirtualFrame;
1011
import com.oracle.truffle.api.source.SourceSection;
@@ -28,8 +29,7 @@ public ClassDecl(SourceSection location, String name, ArrayList<String> fields)
2829

2930
@Override
3031
public Object executeGeneric(VirtualFrame frame) {
31-
// TODO Auto-generated method stub
32-
throw new UnsupportedOperationException("Unimplemented method 'executeGeneric'");
32+
throw LKQLRuntimeException.shouldNotExecute(this);
3333
}
3434

3535
@Override

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/pass/PassExpr.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import com.adacore.lkql_jit.nodes.expressions.Expr;
1111
import com.adacore.lkql_jit.nodes.expressions.value_read.ReadArgument;
1212
import com.adacore.lkql_jit.runtime.values.DynamicAdaNode;
13+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1314
import com.oracle.truffle.api.dsl.Fallback;
1415
import com.oracle.truffle.api.dsl.NodeChild;
1516
import com.oracle.truffle.api.dsl.Specialization;
17+
import com.oracle.truffle.api.frame.MaterializedFrame;
1618
import com.oracle.truffle.api.frame.VirtualFrame;
1719
import com.oracle.truffle.api.source.SourceSection;
1820
import java.util.*;
@@ -59,8 +61,10 @@ protected PassExpr(
5961
@Specialization
6062
public Object onDynamicAdaNode(VirtualFrame frame, DynamicAdaNode input) {
6163
final var typingContext = LKQLLanguage.getContext(this).getTypingContext();
62-
typingContext.addAll(add.classes.stream().map(cd -> cd.name).toList());
63-
final var updatedTree = getUpdatedTree(input, frame);
64+
for (var c : add.classes) {
65+
typingContext.add(c.name);
66+
}
67+
final var updatedTree = getUpdatedTree(input, frame.materialize());
6468
typingContext.removeAll(del.classes);
6569
return updatedTree;
6670
}
@@ -100,7 +104,8 @@ public Optional<Integer> getPreviousSlot() {
100104
// core logic
101105

102106
// bottom up rewriting of the tree
103-
private DynamicAdaNode getUpdatedTree(DynamicAdaNode tree, VirtualFrame frame) {
107+
@TruffleBoundary
108+
private DynamicAdaNode getUpdatedTree(DynamicAdaNode tree, MaterializedFrame frame) {
104109
// recurse on children first (bottom up)
105110
for (var child : List.copyOf(tree.children.entrySet())) {
106111
tree.children.put(child.getKey(), getUpdatedTree(child.getValue(), frame));

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/pass/RunPass.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
package com.adacore.lkql_jit.nodes.pass;
77

88
import com.adacore.lkql_jit.LKQLLanguage;
9+
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
910
import com.adacore.lkql_jit.langkit_translator.passes.Hierarchy;
1011
import com.adacore.lkql_jit.nodes.LKQLNode;
1112
import com.adacore.lkql_jit.runtime.values.AdaNodeProxy;
1213
import com.adacore.lkql_jit.runtime.values.LKQLFunction;
1314
import com.adacore.lkql_jit.utils.functions.FrameUtils;
15+
import com.adacore.lkql_jit.utils.functions.ObjectUtils;
16+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1417
import com.oracle.truffle.api.frame.VirtualFrame;
1518
import com.oracle.truffle.api.interop.ArityException;
1619
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -36,35 +39,38 @@ public RunPass(SourceSection location, int slot) {
3639

3740
@Override
3841
public Object executeGeneric(VirtualFrame frame) {
42+
final var ctx = LKQLLanguage.getContext(this);
43+
3944
// Build pass call-chain
4045
final Deque<LKQLFunction> callChain = new ArrayDeque<>();
4146
callChain.push((LKQLFunction) FrameUtils.readLocal(frame, slot));
4247
while (((PassExpr) callChain.peek().rootNode.getBody()).getPreviousSlot().isPresent()) {
4348
callChain.push(
4449
(LKQLFunction) FrameUtils.readLocal(
4550
frame,
46-
((PassExpr) callChain.peek().rootNode.getBody()).getPreviousSlot().get()
51+
// orElse is necessary to garantee no exception for native image
52+
((PassExpr) callChain.peek().rootNode.getBody()).getPreviousSlot().orElse(0)
4753
)
4854
);
4955
}
5056

5157
// Setup all units roots
52-
final var roots = LKQLLanguage.getContext(this).getAllUnitsRoots();
58+
final var roots = ctx.getAllUnitsRoots();
5359
final var units = new Object[roots.length];
5460
for (int i = 0; i < roots.length; i++) {
55-
if (LKQLLanguage.getContext(this).isVerbose()) {
56-
System.out.println(i + ")\n" + roots[i].dumpTree());
61+
if (ctx.isVerbose()) {
62+
debugAST(i, roots[i].dumpTree());
5763
}
5864
units[i] = AdaNodeProxy.convertAST(roots[i]);
5965
}
6066

61-
LKQLLanguage.getContext(this).setTypingContext(Hierarchy.initial());
67+
ctx.setTypingContext(Hierarchy.initial());
6268

6369
do {
64-
final var pass = callChain.pop();
70+
final var pass = callChain.poll();
6571

66-
if (LKQLLanguage.getContext(this).isVerbose()) {
67-
System.out.println("running pass:" + pass);
72+
if (ctx.isVerbose()) {
73+
ctx.println(ObjectUtils.toString(pass));
6874
}
6975

7076
for (int i = 0; i < units.length; i++) {
@@ -73,11 +79,11 @@ public Object executeGeneric(VirtualFrame frame) {
7379
} catch (
7480
UnsupportedTypeException | ArityException | UnsupportedMessageException e
7581
) {
76-
e.printStackTrace();
82+
LKQLRuntimeException.fromJavaException(e, this);
7783
}
7884

79-
if (LKQLLanguage.getContext(this).isVerbose()) {
80-
System.out.println(i + ")\n" + units[i]);
85+
if (ctx.isVerbose()) {
86+
debugAST(i, units[i]);
8187
}
8288
}
8389
} while (!callChain.isEmpty());
@@ -89,6 +95,11 @@ public int getSlot() {
8995
return slot;
9096
}
9197

98+
@TruffleBoundary
99+
private void debugAST(int num, Object ast) {
100+
System.out.println(num + ")\n" + ast);
101+
}
102+
92103
@Override
93104
public String toString(int indentLevel) {
94105
// TODO Auto-generated method stub

lkql_jit/language/src/main/java/com/adacore/lkql_jit/runtime/values/AdaNodeProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.adacore.langkit_support.LangkitSupport.NodeInterface;
99
import com.adacore.libadalang.Libadalang;
10+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1011
import java.util.HashMap;
1112

1213
/**
@@ -21,10 +22,12 @@ public class AdaNodeProxy extends DynamicAdaNode {
2122
/** The native libadalang node this class proxies to */
2223
public final NodeInterface base;
2324

25+
@TruffleBoundary
2426
public static AdaNodeProxy convertAST(NodeInterface root) {
2527
return new AdaNodeProxy(root);
2628
}
2729

30+
@TruffleBoundary
2831
private AdaNodeProxy(NodeInterface root) {
2932
super(
3033
((Libadalang.AdaNode) root).getClassName(),

lkql_jit/language/src/main/java/com/adacore/lkql_jit/runtime/values/DynamicAdaNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.adacore.lkql_jit.runtime.values;
77

88
import com.adacore.lkql_jit.runtime.values.bases.BasicLKQLValue;
9+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
910
import java.util.HashMap;
1011

1112
/**
@@ -24,6 +25,7 @@ public class DynamicAdaNode extends BasicLKQLValue {
2425
* This constructor is the one used during lowering
2526
* and should be the default one
2627
*/
28+
@TruffleBoundary
2729
public DynamicAdaNode(
2830
String kind,
2931
HashMap<String, DynamicAdaNode> children,
@@ -40,6 +42,7 @@ public DynamicAdaNode(
4042
* This constructor is used only in the context of a nanopass
4143
* by a constructor call
4244
*/
45+
@TruffleBoundary
4346
public DynamicAdaNode(String kind, Object[] args, String[] argnames) {
4447
this.kind = kind;
4548
this.children = new HashMap<>();
@@ -56,6 +59,7 @@ public DynamicAdaNode(String kind, Object[] args, String[] argnames) {
5659
this.isList = false;
5760
}
5861

62+
@TruffleBoundary
5963
public Object getField(String name) {
6064
var res = fields.get(name);
6165
return res != null ? res : children.get(name);

0 commit comments

Comments
 (0)