56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#include "LumeLib.h"
|
|
|
|
#include <llvm/ADT/ArrayRef.h>
|
|
#include <llvm/Support/LogicalResult.h>
|
|
|
|
#include <mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h>
|
|
#include <mlir/Dialect/SPIRV/IR/SPIRVEnums.h>
|
|
#include <mlir/IR/MLIRContext.h>
|
|
#include <mlir/Pass/PassManager.h>
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
|
|
namespace lume {
|
|
|
|
mlir::OwningOpRef<mlir::spirv::ModuleOp>
|
|
CompileSPVToMLIR(std::unique_ptr<llvm::MemoryBuffer> input,
|
|
mlir::MLIRContext &context,
|
|
mlir::spirv::DeserializationOptions &options) {
|
|
if (!input) {
|
|
return nullptr;
|
|
}
|
|
|
|
const auto *start = input->getBufferStart();
|
|
const auto size = input->getBufferSize();
|
|
|
|
if (size % sizeof(uint32_t) != 0) {
|
|
return nullptr;
|
|
}
|
|
|
|
const auto binary = llvm::ArrayRef(
|
|
reinterpret_cast<const uint32_t *>(start),
|
|
size / sizeof(uint32_t));
|
|
|
|
return mlir::spirv::deserialize(binary, &context, options);
|
|
}
|
|
|
|
llvm::LogicalResult ConvertSPVDialectToLLVMDialect(
|
|
mlir::MLIRContext &context,
|
|
mlir::ModuleOp *module) {
|
|
if (!module) {
|
|
return llvm::failure();
|
|
}
|
|
|
|
mlir::ConvertSPIRVToLLVMPassOptions options;
|
|
options.clientAPI = mlir::spirv::ClientAPI::Vulkan;
|
|
|
|
mlir::PassManager pm(&context);
|
|
pm.addPass(mlir::createConvertSPIRVToLLVMPass(options));
|
|
|
|
return pm.run(*module);
|
|
}
|
|
|
|
}
|
|
|