diff --git a/example/passthrough_fragment.glsl b/example/passthrough_fragment.glsl index 04e9738..26fdc79 100644 --- a/example/passthrough_fragment.glsl +++ b/example/passthrough_fragment.glsl @@ -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; } diff --git a/include/CodeGen/MLIRCodeGen.h b/include/CodeGen/MLIRCodeGen.h index a207f53..397bd8d 100644 --- a/include/CodeGen/MLIRCodeGen.h +++ b/include/CodeGen/MLIRCodeGen.h @@ -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; diff --git a/lib/CodeGen/MLIRCodeGen.cpp b/lib/CodeGen/MLIRCodeGen.cpp index 8c76da7..9671792 100644 --- a/lib/CodeGen/MLIRCodeGen.cpp +++ b/lib/CodeGen/MLIRCodeGen.cpp @@ -31,8 +31,8 @@ void MLIRCodeGen::initModuleOp() { spirvModule = cast(Operation::create(state)); } -void MLIRCodeGen::dump() { - spirvModule.dump(); +void MLIRCodeGen::print() { + spirvModule.print(llvm::outs()); } bool MLIRCodeGen::verify() { return !failed(mlir::verify(spirvModule)); } @@ -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); } @@ -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); } @@ -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) { diff --git a/standalone/shaderpulse.cpp b/standalone/shaderpulse.cpp index ea7d9aa..26ed621 100644 --- a/standalone/shaderpulse.cpp +++ b/standalone/shaderpulse.cpp @@ -7,27 +7,6 @@ #include #include -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; @@ -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]; @@ -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(); @@ -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()) { @@ -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; } } diff --git a/test/CodeGen/binary_expressions.glsl b/test/CodeGen/binary_expressions.glsl new file mode 100644 index 0000000..1dea6a9 --- /dev/null +++ b/test/CodeGen/binary_expressions.glsl @@ -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; +} diff --git a/test/CodeGen/run_test.sh b/test/CodeGen/run_test.sh new file mode 100755 index 0000000..e60b8f6 --- /dev/null +++ b/test/CodeGen/run_test.sh @@ -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