Skip to content

Commit 869841b

Browse files
authored
Merge pull request #494 from OpenVADL/bugfix/immediate-bitwidth-branches
lcb: Used predicate method for "isBranchOffsetInRange"
2 parents 668c2fb + 8dddfda commit 869841b

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

vadl/main/resources/templates/lcb/llvm/lib/Target/InstrInfo.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,13 @@ bool [(${namespace})]InstrInfo::isBranchOffsetInRange(unsigned BranchOp, int64_t
356356
default:
357357
std::cerr << "Op " << BranchOp << std::endl;
358358
llvm_unreachable("Unexpected opcode!");
359-
[# th:each="branch : ${branchInstructions}" ]
359+
[# th:each="branch : ${machineBranchInstructions}" ]
360360
case [(${namespace})]::[(${branch.name})]:
361-
return isIntN([(${branch.bitWidth})], BrOffset);
361+
return [(${branch.predicateMethod})](BrOffset);
362+
[/]
363+
[# th:each="branch : ${pseudoBranchInstructions}" ]
364+
case [(${namespace})]::[(${branch.name})]:
365+
return isIntN([(${branch.bitWidth})], BrOffset);
362366
[/]
363367
}
364368
}

vadl/main/vadl/lcb/template/lib/Target/EmitInstrInfoCppFilePass.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import vadl.lcb.passes.isaMatching.database.Query;
4747
import vadl.lcb.template.CommonVarNames;
4848
import vadl.lcb.template.LcbTemplateRenderingPass;
49+
import vadl.lcb.template.utils.ImmediatePredicateFunctionProvider;
4950
import vadl.pass.PassResults;
5051
import vadl.template.Renderable;
5152
import vadl.types.BuiltInTable;
@@ -283,7 +284,23 @@ private Optional<String> getJumpFromMachineInstructions(
283284
.map(Definition::simpleName);
284285
}
285286

286-
record BranchInstruction(String name, /* size of the immediate */ int bitWidth) implements
287+
record BranchInstruction(String name,
288+
int bitWidth, /* size of the immediate */
289+
String predicateMethod) implements
290+
Renderable {
291+
292+
@Override
293+
public Map<String, Object> renderObj() {
294+
return Map.of(
295+
"name", name,
296+
"bitWidth", bitWidth,
297+
"predicateMethod", predicateMethod
298+
);
299+
}
300+
}
301+
302+
record PseudoBranchInstruction(String name,
303+
int bitWidth /* size of the immediate */) implements
287304
Renderable {
288305

289306
@Override
@@ -370,7 +387,9 @@ protected Map<String, Object> createVariables(final PassResults passResults,
370387
map.put("additionImm", additionRI.simpleName());
371388
map.put("addition", additionRR.simpleName());
372389
map.put("additionRegisterFile", additionRegisterFile.simpleName());
373-
map.put("branchInstructions", getBranchInstructions(specification, passResults, fieldUsages));
390+
map.put("machineBranchInstructions", machineBranchInstructions(specification, passResults));
391+
map.put("pseudoBranchInstructions",
392+
pseudoBranchInstructions(specification, passResults, fieldUsages));
374393
map.put("instructionSizes", instructionSizes(specification));
375394
map.put("jumpInstruction", jumpInstructionName);
376395
map.put("beq",
@@ -415,14 +434,24 @@ private List<InstructionSize> instructionSizes(Specification specification) {
415434
.toList();
416435
}
417436

418-
private List<BranchInstruction> getBranchInstructions(
437+
private List<BranchInstruction> machineBranchInstructions(
438+
Specification specification,
439+
PassResults passResults) {
440+
var branchInstructions = new ArrayList<BranchInstruction>();
441+
var database = new Database(passResults, specification);
442+
443+
machineInstructions(passResults, database, branchInstructions);
444+
445+
return branchInstructions;
446+
}
447+
448+
private List<PseudoBranchInstruction> pseudoBranchInstructions(
419449
Specification specification,
420450
PassResults passResults,
421451
IdentifyFieldUsagePass.ImmediateDetectionContainer fieldUsages) {
422-
var branchInstructions = new ArrayList<BranchInstruction>();
452+
var branchInstructions = new ArrayList<PseudoBranchInstruction>();
423453
var database = new Database(passResults, specification);
424454

425-
machineInstructions(database, branchInstructions);
426455
pseudoInstructions(fieldUsages, database, branchInstructions);
427456

428457
return branchInstructions;
@@ -431,7 +460,7 @@ private List<BranchInstruction> getBranchInstructions(
431460
private static void pseudoInstructions(
432461
IdentifyFieldUsagePass.ImmediateDetectionContainer fieldUsages,
433462
Database database,
434-
List<BranchInstruction> branchInstructions) {
463+
List<PseudoBranchInstruction> branchInstructions) {
435464
var result = database.run(
436465
new Query.Builder().pseudoInstructionLabel(PseudoInstructionLabel.J).build());
437466

@@ -447,14 +476,17 @@ private static void pseudoInstructions(
447476
var immediate = unwrap(immediates.stream().findFirst());
448477
int bitWidth = immediate.size();
449478
branchInstructions.add(
450-
new BranchInstruction(pseudoInstruction.identifier.simpleName(), bitWidth));
479+
new PseudoBranchInstruction(pseudoInstruction.identifier.simpleName(), bitWidth));
451480
}
452481
}
453482
}
454483

455484
private static void machineInstructions(
485+
PassResults passResults,
456486
Database database,
457487
List<BranchInstruction> branchInstructions) {
488+
var lookup =
489+
ImmediatePredicateFunctionProvider.predicateFunctionsByFieldAccess(passResults);
458490
var result = database.run(new Query.Builder().machineInstructionLabels(List.of(
459491
MachineInstructionLabel.J,
460492
MachineInstructionLabel.BEQ,
@@ -490,9 +522,14 @@ private static void machineInstructions(
490522
() -> Diagnostic.error("We only support branch instructions with one label.",
491523
machineInstruction.location()));
492524
var immediate = unwrap(immediates.stream().findFirst());
525+
var function = ensureNonNull(lookup.get(immediate.fieldAccess()),
526+
() -> Diagnostic.error("Cannot find field access' predicate", immediate.location()));
527+
var functionName = function.header().functionName().lower();
493528
int bitWidth = immediate.fieldAccess().type().asDataType().bitWidth();
494529
branchInstructions.add(
495-
new BranchInstruction(machineInstruction.identifier.simpleName(), bitWidth));
530+
new BranchInstruction(machineInstruction.identifier.simpleName(),
531+
bitWidth,
532+
functionName));
496533
}
497534
}
498535

vadl/main/vadl/lcb/template/utils/ImmediatePredicateFunctionProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package vadl.lcb.template.utils;
1818

19+
import java.util.IdentityHashMap;
1920
import java.util.Map;
2021
import vadl.cppCodeGen.model.GcbCppFunctionWithBody;
2122
import vadl.lcb.passes.llvmLowering.CreateFunctionsFromImmediatesPass;
2223
import vadl.lcb.passes.llvmLowering.tablegen.model.TableGenImmediateRecord;
2324
import vadl.pass.PassResults;
25+
import vadl.viam.Format;
2426

2527
/**
2628
* Utility class for predicates.
@@ -34,4 +36,19 @@ public static Map<TableGenImmediateRecord, GcbCppFunctionWithBody> generatePredi
3436
return ((CreateFunctionsFromImmediatesPass.Output)
3537
passResults.lastResultOf(CreateFunctionsFromImmediatesPass.class)).predicates();
3638
}
39+
40+
/**
41+
* Get the predicate functions by field access.
42+
*/
43+
public static Map<Format.FieldAccess, GcbCppFunctionWithBody> predicateFunctionsByFieldAccess(
44+
PassResults passResults) {
45+
var result = new IdentityHashMap<Format.FieldAccess, GcbCppFunctionWithBody>();
46+
var map = generatePredicateFunctions(passResults);
47+
48+
for (var entry : map.entrySet()) {
49+
result.put(entry.getKey().fieldAccessRef(), entry.getValue());
50+
}
51+
52+
return result;
53+
}
3754
}

0 commit comments

Comments
 (0)