Skip to content

Commit

Permalink
Start work on FileCheck based code generation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 22, 2024
1 parent 58131e1 commit a60f2c5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 42 deletions.
5 changes: 0 additions & 5 deletions example/passthrough_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ struct Color {

void main() {
Color color = Color(1.0, 0.0, 0.0, 1.0);
uint b = 1u;
uint c = 2u;
bool a = c < b;
float f = 1.0;
float g = -f;
outColor = vec4(color.r, color.g, color.b, color.a);
return;
}
2 changes: 1 addition & 1 deletion include/CodeGen/MLIRCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MLIRCodeGen : public ASTVisitor {
public:
MLIRCodeGen();
void initModuleOp();
void dump();
void print();
bool verify();
void visit(TranslationUnit *) override;
void visit(BinaryExpression *) override;
Expand Down
9 changes: 2 additions & 7 deletions lib/CodeGen/MLIRCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void MLIRCodeGen::initModuleOp() {
spirvModule = cast<spirv::ModuleOp>(Operation::create(state));
}

void MLIRCodeGen::dump() {
spirvModule.dump();
void MLIRCodeGen::print() {
spirvModule.print(llvm::outs());
}

bool MLIRCodeGen::verify() { return !failed(mlir::verify(spirvModule)); }
Expand Down Expand Up @@ -288,8 +288,6 @@ void MLIRCodeGen::declare(SymbolTableEntry entry) {
if (symbolTable.count(entry.variable->getIdentifierName()))
return;


std::cout << "Declaring " << entry.variable->getIdentifierName() << std::endl;
symbolTable.insert(entry.variable->getIdentifierName(), entry);
}

Expand Down Expand Up @@ -345,7 +343,6 @@ void MLIRCodeGen::createVariable(shaderpulse::Type *type,
// builder.getUnitAttr());
} else {
if (varDecl->getInitialzerExpression()) {
std::cout << "Accept init" << std::endl;
varDecl->getInitialzerExpression()->accept(this);
}

Expand Down Expand Up @@ -643,11 +640,9 @@ void MLIRCodeGen::visit(CallExpression *callExp) {
}

void MLIRCodeGen::visit(VariableExpression *varExp) {
std::cout << "Looking up " << varExp->getName() << std::endl;
auto entry = symbolTable.lookup(varExp->getName());

if (entry.variable) {
std::cout << "Looked up and found " << varExp->getName() << std::endl;
Value val;

if (entry.isGlobal) {
Expand Down
41 changes: 12 additions & 29 deletions standalone/shaderpulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,6 @@
#include <iostream>
#include <fstream>

static std::string functionDeclarationTestString =
R"(
uniform highp float a;
uniform int b;
uint c;
vec3 d;
mat2x2 e;
float foo() {
return 1.0;
}
float myFunc(vec2 arg1, bool arg2) {
float f;
float g;
f = 1.0;
g = f + 2.0;
return g + foo();
}
)";

using namespace shaderpulse;
using namespace shaderpulse::ast;
using namespace shaderpulse::lexer;
Expand All @@ -39,9 +18,10 @@ int main(int argc, char** argv) {
std::cout << "Missing input file." << std::endl;
return -1;
}

bool printAST = false;
bool codeGen = true;
bool analyze = true;

for (size_t i = 2; i < argc; i++) {
std::string arg = argv[i];
Expand All @@ -50,13 +30,14 @@ int main(int argc, char** argv) {
printAST = true;
} else if (arg == "--no-codegen") {
codeGen = false;
} else if (arg == "--no-analyze") {
analyze = false;
} else {
std::cout << "Unrecognized argument: '" << arg << "'." << std::endl;
return -1;
}
}


std::ifstream glslIn(argv[1]);
std::stringstream shaderCodeBuffer;
shaderCodeBuffer << glslIn.rdbuf();
Expand All @@ -65,7 +46,6 @@ int main(int argc, char** argv) {
auto preprocessor = preprocessor::Preprocessor(sourceCode);
preprocessor.process();
auto processedCode = preprocessor.getProcessedSource();
std::cout << processedCode;
auto lexer = Lexer(processedCode);
auto resp = lexer.lexCharacterStream();
if (!resp.has_value()) {
Expand All @@ -83,14 +63,17 @@ int main(int argc, char** argv) {
}

if (codeGen) {
auto analyzer = SemanticAnalyzer();
translationUnit->accept(&analyzer);
if (analyze) {
auto analyzer = SemanticAnalyzer();
translationUnit->accept(&analyzer);
}

auto mlirCodeGen = codegen::MLIRCodeGen();
translationUnit->accept(&mlirCodeGen);
mlirCodeGen.dump();
mlirCodeGen.print();

if (mlirCodeGen.verify()) {
std::cout << "SPIR-V module verified" << std::endl;
if (!mlirCodeGen.verify()) {
std::cout << "Error verifying the SPIR-V module" << std::endl;
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/CodeGen/binary_expressions.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void main() {
// CHECK: %0 = spirv.IAdd %cst1_si32, %cst2_si32 : si3
int a = 1 + 2;

// CHECK: %2 = spirv.IAdd %cst1_ui32, %cst2_ui32 : ui32
uint c = 1u + 2u;

// CHECK: %4 = spirv.FAdd %cst_f32, %cst_f32_0 : f32
float b = 1.0f + 2.0f;

// CHECK: %6 = spirv.FAdd %cst_f64, %cst_f64_1 : f64
double d = 1.0lf + 2.0lf;

return;
}
33 changes: 33 additions & 0 deletions test/CodeGen/run_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -e

SHADERPULSE="../../build/shaderpulse-standalone"
FILECHECK="../../llvm-project/build/bin/FileCheck"

if [ ! -x "$SHADERPULSE" ]; then
echo "Error: shaderpulse binary not found at $SHADERPULSE"
exit 1
fi

if [ ! -x "$FILECHECK" ]; then
echo "Error: FileCheck binary not found at $FILECHECK"
exit 1
fi

for TEST_FILE in *.glsl; do
if [ ! -f "$TEST_FILE" ]; then
echo "No .glsl files found in the current directory."
exit 1
fi

echo "Running test on $TEST_FILE"
$SHADERPULSE "$TEST_FILE" --no-analyze | $FILECHECK "$TEST_FILE"

if [ $? -eq 0 ]; then
echo "Test passed for $TEST_FILE"
else
echo "Test failed for $TEST_FILE"
exit 1
fi
done

0 comments on commit a60f2c5

Please sign in to comment.