Skip to content

Commit

Permalink
Compile to spirv
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Oct 2, 2024
1 parent 622f9bc commit 3ab30ac
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
build
.vscode
test/CodeGen/*.mlir
example/*.mlir
example/*.mlir
*.spv
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ if(ENABLE_CODEGEN)
MLIRLLVMToLLVMIRTranslation
MLIRMemRefDialect
MLIRSPIRVDialect
MLIRSPIRVSerialization
MLIRParser
MLIRPass
MLIRSideEffectInterfaces
Expand Down
16 changes: 12 additions & 4 deletions compiler/shaderpulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,21 @@ int main(int argc, char** argv) {
mlirCodeGen.print();

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

bool success = mlirCodeGen.saveToFile(outputPath);

if (!success) {
std::cout << "Failed to save spirv mlir to file.";
return -1;
}

bool succes = mlirCodeGen.saveToFile(outputPath);
success = mlirCodeGen.emitSpirv(outputPath.replace_extension(".spv"));

if (!succes) {
if (!success) {
std::cout << "Failed to emit spirv binary.";
return -1;
}
}
Expand Down
14 changes: 14 additions & 0 deletions example/square.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) buffer InBuffer {
int data[];
};

layout(binding = 1) buffer OutBuffer {
int result[];
};

void main() {
uint idx = gl_GlobalInvocationID.x;
result[idx] = data[idx] * data[idx];
}
1 change: 1 addition & 0 deletions include/CodeGen/MLIRCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MLIRCodeGen : public ASTVisitor {
void initModuleOp();
void print();
bool saveToFile(const std::filesystem::path& outputPath);
bool emitSpirv(const std::filesystem::path& outputPath);
bool verify();
void visit(TranslationUnit *) override;
void visit(BinaryExpression *) override;
Expand Down
14 changes: 14 additions & 0 deletions lib/CodeGen/MLIRCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <cassert>
#include <fstream>
#include "mlir/Target/SPIRV/Serialization.h"
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/MemoryBuffer.h>
#include <utility>
Expand Down Expand Up @@ -213,6 +214,19 @@ bool MLIRCodeGen::saveToFile(const std::filesystem::path& outputPath) {
return true;
}

bool MLIRCodeGen::emitSpirv(const std::filesystem::path& outputPath) {
llvm::SmallVector<uint32_t, 128> spirvBinary;
mlir::LogicalResult result = mlir::spirv::serialize(spirvModule, spirvBinary);
if (failed(result)) {
std::cerr << "Failed to serialize SPIR-V module." << std::endl;
return false;
}

std::ofstream outFile(outputPath, std::ios::binary);
outFile.write(reinterpret_cast<const char*>(spirvBinary.data()), spirvBinary.size() * sizeof(uint32_t));
return true;
}

bool MLIRCodeGen::verify() { return !failed(mlir::verify(spirvModule)); }

void MLIRCodeGen::insertEntryPoint() {
Expand Down

0 comments on commit 3ab30ac

Please sign in to comment.