summaryrefslogtreecommitdiff
path: root/willow/unittest/IR
diff options
context:
space:
mode:
authorStefan Weigl-Bosker <stefan@s00.xyz>2026-02-19 18:45:44 -0500
committerStefan Weigl-Bosker <stefan@s00.xyz>2026-02-19 18:45:44 -0500
commitaf3d0ad1926eb825f64152cf217fc9a4777f0be3 (patch)
tree8d7f73ce6c3c89863418382d8d553a06c668bbb3 /willow/unittest/IR
parentd11fbc8268f5775ad783f8570478daad4a9e81cf (diff)
downloadcompiler-af3d0ad1926eb825f64152cf217fc9a4777f0be3.tar.gz
[willow]: more cleanup, testsmore-tests
Diffstat (limited to 'willow/unittest/IR')
-rw-r--r--willow/unittest/IR/BUILD.bazel13
-rw-r--r--willow/unittest/IR/InstructionsTest.cpp141
-rw-r--r--willow/unittest/IR/VerifierTest.cpp15
3 files changed, 160 insertions, 9 deletions
diff --git a/willow/unittest/IR/BUILD.bazel b/willow/unittest/IR/BUILD.bazel
index b41dfcd..e25efbf 100644
--- a/willow/unittest/IR/BUILD.bazel
+++ b/willow/unittest/IR/BUILD.bazel
@@ -8,9 +8,20 @@ cc_test(
tags = ["ir"]
)
+cc_test(
+ name = "instructions",
+ srcs = ["InstructionsTest.cpp"],
+ deps = [
+ "//willow",
+ "@catch2//:catch2_main"
+ ],
+ tags = ["ir"]
+)
+
test_suite(
name = "ir_tests",
tests = [
- ":verifier"
+ ":verifier",
+ ":instructions"
],
)
diff --git a/willow/unittest/IR/InstructionsTest.cpp b/willow/unittest/IR/InstructionsTest.cpp
new file mode 100644
index 0000000..acb83a1
--- /dev/null
+++ b/willow/unittest/IR/InstructionsTest.cpp
@@ -0,0 +1,141 @@
+#include <catch2/catch_test_macros.hpp>
+
+#include <willow/IR/Context.h>
+#include <willow/IR/Diagnostic.h>
+#include <willow/IR/DiagnosticEngine.h>
+#include <willow/IR/IRBuilder.h>
+#include <willow/IR/Instructions.h>
+#include <willow/IR/Verifier.h>
+
+#include <iostream>
+
+using namespace willow;
+
+TEST_CASE("valid arithmetic instructions") {
+ WillowContext ctx;
+ DiagnosticEngine eng;
+ TypeContext &types = ctx.types();
+ ConstantPool &consts = ctx.constants();
+
+ Type i64Ty = types.IntType(64);
+ Type i34Ty = types.IntType(34);
+ Type voidTy = types.VoidType();
+ Type voidFnTy = types.FunctionType(voidTy, {});
+
+ Value *one64 = consts.getInt(i64Ty, 1);
+ Value *two64 = consts.getInt(i64Ty, 2);
+ Value *twelve34 = consts.getInt(i34Ty, 12);
+
+ Module *m = ctx.addModule("testmod");
+
+ Function *fn = m->emplaceFunction("fn", m, voidFnTy);
+
+ eng.setHandler([](const Diagnostic& d) { std::cout << d << "\n"; });
+
+ BasicBlock *bb =
+ fn->addBlock(std::make_unique<BasicBlock>(fn, types.BasicBlockType()));
+ IRBuilder builder{ctx, *bb, bb->begin()};
+
+ SECTION("Valid add instruction") {
+ auto &inst = *builder.buildAdd(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Add);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid mul instruction") {
+ auto &inst = *builder.buildMul(i34Ty, twelve34, twelve34);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Mul);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid sub instruction") {
+ auto &inst = *builder.buildSub(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Sub);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid div instruction") {
+ auto &inst = *builder.buildDiv(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Div);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid mod instruction") {
+ auto &inst = *builder.buildMod(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Mod);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid shl instruction") {
+ auto &inst = *builder.buildShl(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Shl);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid shr instruction") {
+ auto &inst = *builder.buildShr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Shr);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ashl instruction") {
+ auto &inst = *builder.buildAshl(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ashl);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ashr instruction") {
+ auto &inst = *builder.buildAshr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ashr);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid and instruction") {
+ auto &inst = *builder.buildAnd(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::And);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid not instruction") {
+ auto &inst = *builder.buildNot(i34Ty, twelve34);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Not);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid or instruction") {
+ auto &inst = *builder.buildOr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Or);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid eq Instruction") {
+ auto &inst = *builder.buildEq(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Eq);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid lt Instruction") {
+ auto &inst = *builder.buildEq(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Eq);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid gt instruction") {
+ auto &inst = *builder.buildGt(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Gt);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid le instruction") {
+ auto &inst = *builder.buildLe(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Le);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ge instruction") {
+ auto &inst = *builder.buildGe(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ge);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+}
diff --git a/willow/unittest/IR/VerifierTest.cpp b/willow/unittest/IR/VerifierTest.cpp
index efe34db..1b71eb8 100644
--- a/willow/unittest/IR/VerifierTest.cpp
+++ b/willow/unittest/IR/VerifierTest.cpp
@@ -38,8 +38,7 @@ TEST_CASE("valid function", "[verifier]") {
TEST_CASE("invalid basic block", "[verifier]") {
WillowContext ctx;
- std::vector<Diagnostic> diags;
- DiagnosticEngine eng([&](Diagnostic d) { diags.push_back(std::move(d)); });
+ DiagnosticEngine eng;
Type i64Ty = ctx.types().IntType(64);
Type voidTy = ctx.types().VoidType();
@@ -60,22 +59,22 @@ TEST_CASE("invalid basic block", "[verifier]") {
}
SECTION("Basic block with no terminator") {
- builder.BuildAdd(i64Ty, one, one);
+ builder.buildAdd(i64Ty, one, one);
REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng)));
}
SECTION("Teminator must be the last instruction in a basic block") {
- builder.BuildCall(i64Ty, &fn2);
- builder.BuildAdd(i64Ty, one, one);
+ builder.buildCall(i64Ty, &fn2);
+ builder.buildAdd(i64Ty, one, one);
REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng)));
}
SECTION("Basic block with invalid instruction") {
auto *bb2 = fn.addBlock(
std::make_unique<BasicBlock>(&fn, ctx.types().BasicBlockType()));
- builder.SetInsertPoint(bb2->end());
- builder.BuildAdd(voidTy, one, one);
- builder.BuildRet();
+ builder.setInsertPoint(bb2->end());
+ builder.buildAdd(voidTy, one, one);
+ builder.buildRet();
REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng)));
}