spice_packages/tpde/tpde.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #![core.compiler.warnings.ignore] | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/vector"; | ||
| 5 | import "std/io/file"; | ||
| 6 | |||
| 7 | // Package imports | ||
| 8 | // Resolved against the dir of the main source file, where Spice Shaker installs the llvm dependency next to this package | ||
| 9 | import "@/spice_packages/llvm/llvm" as llvm; | ||
| 10 | |||
| 11 | // Own imports | ||
| 12 | import "src/linker-flags"; | ||
| 13 | |||
| 14 | // ===== External type definitions ===== | ||
| 15 | type VoidPtr alias byte*; | ||
| 16 | type LLVMBool alias int; | ||
| 17 | type LLVMModuleRef alias VoidPtr; | ||
| 18 | type LLVMMemoryBufferRef alias VoidPtr; | ||
| 19 | type TPDECompilerRef alias VoidPtr; | ||
| 20 | |||
| 21 | // ===== External functions ===== | ||
| 22 | // TPDE C API wrapper (see tpde-wrapper.cpp) | ||
| 23 | ext f<LLVMBool> TPDEIsAvailable(); | ||
| 24 | ext f<TPDECompilerRef> TPDECreateCompiler(string /*Triple*/); | ||
| 25 | ext p TPDEDisposeCompiler(TPDECompilerRef /*Compiler*/); | ||
| 26 | ext f<LLVMBool> TPDECompileToObjectFile(TPDECompilerRef /*Compiler*/, LLVMModuleRef /*M*/, string /*Filename*/, string* /*ErrorMessage*/); | ||
| 27 | ext f<LLVMBool> TPDECompileToMemoryBuffer(TPDECompilerRef /*Compiler*/, LLVMModuleRef /*M*/, LLVMMemoryBufferRef* /*OutMemBuf*/, string* /*ErrorMessage*/); | ||
| 28 | // LLVM C API | ||
| 29 | ext f<byte*> LLVMGetBufferStart(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 30 | ext f<unsigned long> LLVMGetBufferSize(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 31 | ext p LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 32 | ext p LLVMDisposeMessage(string /*Message*/); | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Check if these bindings are backed by TPDE. They are not on platforms TPDE does not support (TPDE only emits ELF objects | ||
| 36 | * for x86_64 and aarch64) and when TPDE_FLAGS was not set while linking. | ||
| 37 | * | ||
| 38 | * @return TPDE available or not | ||
| 39 | */ | ||
| 40 | 15 | public f<bool> isAvailable() { | |
| 41 | 15 | return TPDEIsAvailable() != 0; | |
| 42 | } | ||
| 43 | |||
| 44 | // ===== Compiler ===== | ||
| 45 | |||
| 46 | /** | ||
| 47 | * TPDE compiler for a specific target, like tpde_llvm::LLVMCompiler. It compiles LLVM modules to ELF objects with -O0-quality | ||
| 48 | * code, but a lot faster than the LLVM back end. | ||
| 49 | */ | ||
| 50 | public type Compiler struct { | ||
| 51 | TPDECompilerRef self | ||
| 52 | llvm::Triple triple | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Create a TPDE compiler for the given target triple. Use isValid() to check if TPDE supports the triple. | ||
| 57 | * | ||
| 58 | * @param triple Target triple | ||
| 59 | */ | ||
| 60 | 5 | public p Compiler.ctor(const llvm::Triple& triple) { | |
| 61 | 5 | this.triple = triple; | |
| 62 | 5 | this.self = TPDECreateCompiler(this.triple.value); | |
| 63 | 5 | } | |
| 64 | |||
| 65 | /** | ||
| 66 | * A compiler owns its TPDE compiler, so copies create their own one for the same target. Otherwise, they would share it and | ||
| 67 | * it would be disposed as soon as the first of the copies goes out of scope. | ||
| 68 | * | ||
| 69 | * @param other Compiler to copy | ||
| 70 | */ | ||
| 71 | 2 | public p Compiler.ctor(const Compiler& other) { | |
| 72 | 2 | this.triple = other.triple; | |
| 73 | 2 | this.self = nil<TPDECompilerRef>; | |
| 74 | 2 | if other.self != nil<TPDECompilerRef> { | |
| 75 | 1 | this.self = TPDECreateCompiler(this.triple.value); | |
| 76 | 1 | } | |
| 77 | 2 | } | |
| 78 | |||
| 79 | /** | ||
| 80 | * Assign another compiler. Like the copy constructor, this creates an own TPDE compiler for the target of the other one. | ||
| 81 | * | ||
| 82 | * @param other Compiler to assign | ||
| 83 | */ | ||
| 84 | 1 | public p operator=(Compiler& this, const Compiler& other) { | |
| 85 | // Valid compilers never share a TPDE compiler, so the same one means self-assignment | ||
| 86 | 1 | if this.self != nil<TPDECompilerRef> && this.self == other.self { | |
| 87 | ✗ | return; | |
| 88 | } | ||
| 89 | 1 | if this.self != nil<TPDECompilerRef> { | |
| 90 | ✗ | TPDEDisposeCompiler(this.self); | |
| 91 | ✗ | } | |
| 92 | 1 | this.triple = other.triple; | |
| 93 | 1 | this.self = nil<TPDECompilerRef>; | |
| 94 | 1 | if other.self != nil<TPDECompilerRef> { | |
| 95 | 1 | this.self = TPDECreateCompiler(this.triple.value); | |
| 96 | 1 | } | |
| 97 | 1 | } | |
| 98 | |||
| 99 | /** | ||
| 100 | * Dispose the TPDE compiler | ||
| 101 | */ | ||
| 102 | 7 | public p Compiler.dtor() { | |
| 103 | 7 | if this.self != nil<TPDECompilerRef> { | |
| 104 | 5 | TPDEDisposeCompiler(this.self); | |
| 105 | 5 | this.self = nil<TPDECompilerRef>; | |
| 106 | 5 | } | |
| 107 | 7 | } | |
| 108 | |||
| 109 | /** | ||
| 110 | * Check if the compiler could be created. This is not the case, if TPDE does not support the target triple or if the | ||
| 111 | * bindings are not backed by TPDE. | ||
| 112 | * | ||
| 113 | * @return Valid or not | ||
| 114 | */ | ||
| 115 | 18 | public const f<bool> Compiler.isValid() { | |
| 116 | 18 | return this.self != nil<TPDECompilerRef>; | |
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Compile the given module to an ELF object file. The module might be modified during compilation. | ||
| 121 | * | ||
| 122 | * @param module Module to compile | ||
| 123 | * @param filename Output file path | ||
| 124 | * @param errorMessage Error message, if the compilation failed. Cleared if it succeeded | ||
| 125 | * @return True if the compilation failed | ||
| 126 | */ | ||
| 127 | 2 | public f<bool> Compiler.compileToFile(llvm::Module& module, string filename, String& errorMessage) { | |
| 128 | 2 | if !this.isValid() { | |
| 129 | 1 | errorMessage = String("The TPDE compiler is not valid"); | |
| 130 | 1 | return true; | |
| 131 | 1 | } | |
| 132 | 1 | errorMessage.clear(); | |
| 133 | 1 | string outMessage = nil<string>; | |
| 134 | 1 | const bool errorOccurred = TPDECompileToObjectFile(this.self, module.getRef(), filename, &outMessage) != 0; | |
| 135 | 1 | if outMessage != nil<string> { | |
| 136 | ✗ | errorMessage = String(outMessage); | |
| 137 | ✗ | LLVMDisposeMessage(outMessage); | |
| 138 | ✗ | } | |
| 139 | 1 | return errorOccurred; | |
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Compile the given module to an ELF object in memory. The module might be modified during compilation. | ||
| 144 | * | ||
| 145 | * @param module Module to compile | ||
| 146 | * @param output Bytes of the emitted object | ||
| 147 | * @param errorMessage Error message, if the compilation failed. Cleared if it succeeded | ||
| 148 | * @return True if the compilation failed | ||
| 149 | */ | ||
| 150 | 9 | public f<bool> Compiler.compileToBuffer(llvm::Module& module, Vector<byte>& output, String& errorMessage) { | |
| 151 | 9 | if !this.isValid() { | |
| 152 | 2 | errorMessage = String("The TPDE compiler is not valid"); | |
| 153 | 2 | return true; | |
| 154 | 2 | } | |
| 155 | 7 | errorMessage.clear(); | |
| 156 | 7 | string outMessage = nil<string>; | |
| 157 | 7 | LLVMMemoryBufferRef buffer = nil<LLVMMemoryBufferRef>; | |
| 158 | 7 | const bool errorOccurred = TPDECompileToMemoryBuffer(this.self, module.getRef(), &buffer, &outMessage) != 0; | |
| 159 | 7 | if outMessage != nil<string> { | |
| 160 | ✗ | errorMessage = String(outMessage); | |
| 161 | ✗ | LLVMDisposeMessage(outMessage); | |
| 162 | ✗ | } | |
| 163 | 7 | if errorOccurred { | |
| 164 | ✗ | return true; | |
| 165 | } | ||
| 166 | // Copy the object bytes out of the buffer. They can contain zero bytes, so the buffer can't be treated as a string | ||
| 167 | 7 | const byte* bufferStart = LLVMGetBufferStart(buffer); | |
| 168 | 7 | const unsigned long bufferSize = LLVMGetBufferSize(buffer); | |
| 169 | 7 | output.clear(); | |
| 170 | 7 | output.reserve(bufferSize); | |
| 171 | unsafe { | ||
| 172 | 10903 | for unsigned long i = 0l; i < bufferSize; i++ { | |
| 173 | 10896 | output.pushBack(bufferStart[i]); | |
| 174 | 10896 | } | |
| 175 | } | ||
| 176 | 7 | LLVMDisposeMemoryBuffer(buffer); | |
| 177 | 7 | return false; | |
| 178 | } | ||
| 179 | |||
| 180 | // ===== Tests ===== | ||
| 181 | |||
| 182 | // Build a module with a function, that adds its two arguments | ||
| 183 | 1 | f<llvm::Function> buildTestAddFunction(llvm::LLVMContext& context, llvm::Module& module) { | |
| 184 | 1 | llvm::IRBuilder builder = llvm::IRBuilder(context); | |
| 185 | 1 | Vector<llvm::Type> argTypes; | |
| 186 | 1 | argTypes.pushBack(builder.getInt32Ty()); | |
| 187 | 1 | argTypes.pushBack(builder.getInt32Ty()); | |
| 188 | 1 | llvm::Type funcType = llvm::getFunctionType(builder.getInt32Ty(), argTypes); | |
| 189 | 1 | llvm::Function func = llvm::Function(module, "add", funcType); | |
| 190 | 1 | func.setLinkage(llvm::LLVMLinkage::ExternalLinkage); | |
| 191 | |||
| 192 | 1 | llvm::BasicBlock entry = llvm::BasicBlock(context, ""); | |
| 193 | 1 | func.pushBack(entry); | |
| 194 | 1 | builder.setInsertPoint(entry); | |
| 195 | 1 | llvm::Value sum = builder.createAdd(func.getArg(0u), func.getArg(1u), "sum"); | |
| 196 | 1 | builder.createRet(sum); | |
| 197 | 2 | return func; | |
| 198 | 1 | } | |
| 199 | |||
| 200 | // Compiles with a copy of the compiler, that is disposed when the function returns | ||
| 201 | 1 | f<unsigned long> compileWithTestCopy(const Compiler& original, llvm::Module& module) { | |
| 202 | 1 | Compiler copy = original; | |
| 203 | 1 | assert copy.isValid(); | |
| 204 | 1 | Vector<byte> objectBytes; | |
| 205 | 1 | String errorMessage; | |
| 206 | 1 | assert !copy.compileToBuffer(module, objectBytes, errorMessage); | |
| 207 | 2 | return objectBytes.getSize(); | |
| 208 | 1 | } | |
| 209 | |||
| 210 | 1 | #[test] | |
| 211 | f<bool> testUnsupportedTarget() { | ||
| 212 | // TPDE does not support WebAssembly. Without TPDE, no compiler is valid | ||
| 213 | 1 | llvm::LLVMContext context; | |
| 214 | 1 | llvm::Module module = llvm::Module("tpde-test", context); | |
| 215 | 1 | Compiler wasmCompiler = Compiler(llvm::Triple("wasm32-unknown-unknown")); | |
| 216 | 1 | assert !wasmCompiler.isValid(); | |
| 217 | 1 | Vector<byte> objectBytes; | |
| 218 | 1 | String errorMessage; | |
| 219 | 1 | assert wasmCompiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 220 | 1 | assert errorMessage == "The TPDE compiler is not valid"; | |
| 221 | 1 | assert wasmCompiler.compileToFile(module, "./tpde-test-wasm.o", errorMessage); | |
| 222 | 1 | assert errorMessage == "The TPDE compiler is not valid"; | |
| 223 | |||
| 224 | // Copies of an invalid compiler are invalid as well | ||
| 225 | 1 | Compiler copy = wasmCompiler; | |
| 226 | 1 | assert !copy.isValid(); | |
| 227 | 1 | return true; | |
| 228 | 1 | } | |
| 229 | |||
| 230 | 1 | #[test] | |
| 231 | f<bool> testCompileModule() { | ||
| 232 | // TPDE is only available on Linux and if the TPDE libraries are linked (see src/linker-flags.spice) | ||
| 233 | 1 | if !isAvailable() { | |
| 234 | ✗ | printf("TPDE is not available, skipping\n"); | |
| 235 | ✗ | return true; | |
| 236 | } | ||
| 237 | |||
| 238 | 1 | llvm::initializeNativeTarget(); | |
| 239 | 1 | llvm::initializeNativeAsmPrinter(); | |
| 240 | 1 | const llvm::Triple targetTriple = llvm::getDefaultTargetTriple(); | |
| 241 | 1 | string error; | |
| 242 | 1 | llvm::Target target = llvm::getTargetFromTriple(targetTriple, &error); | |
| 243 | 1 | llvm::TargetMachine targetMachine = target.createTargetMachine(targetTriple, "generic", "", llvm::LLVMCodeGenOptLevel::None, llvm::LLVMRelocMode::PIC, llvm::LLVMCodeModel::Default); | |
| 244 | |||
| 245 | 1 | llvm::LLVMContext context; | |
| 246 | 1 | llvm::Module module = llvm::Module("tpde-test", context); | |
| 247 | 1 | module.setDataLayout(targetMachine.createDataLayout()); | |
| 248 | 1 | module.setTargetTriple(targetTriple); | |
| 249 | 1 | llvm::Function addFunc = buildTestAddFunction(context, module); | |
| 250 | 1 | assert !llvm::verifyFunction(addFunc); | |
| 251 | |||
| 252 | 1 | Compiler compiler = Compiler(targetTriple); | |
| 253 | 1 | assert compiler.isValid(); | |
| 254 | |||
| 255 | // Compile to memory and check, that the result is a 64-bit ELF object | ||
| 256 | 1 | Vector<byte> objectBytes; | |
| 257 | 1 | String errorMessage; | |
| 258 | 1 | assert !compiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 259 | 1 | assert errorMessage.isEmpty(); | |
| 260 | 1 | assert objectBytes.getSize() > 64ul; // Larger than the ELF header | |
| 261 | 1 | assert cast<int>(objectBytes.get(0ul)) == 0x7F; | |
| 262 | 1 | assert cast<char>(objectBytes.get(1ul)) == 'E'; | |
| 263 | 1 | assert cast<char>(objectBytes.get(2ul)) == 'L'; | |
| 264 | 1 | assert cast<char>(objectBytes.get(3ul)) == 'F'; | |
| 265 | 1 | assert cast<int>(objectBytes.get(4ul)) == 2; // ELFCLASS64 | |
| 266 | 1 | assert cast<int>(objectBytes.get(16ul)) == 1; // ET_REL (relocatable object) | |
| 267 | |||
| 268 | // Compile to a file and check, that it has the same size | ||
| 269 | 1 | const string objectPath = "./tpde-test.o"; | |
| 270 | 1 | assert !compiler.compileToFile(module, objectPath, errorMessage); | |
| 271 | 1 | assert fileExists(objectPath); | |
| 272 | 1 | const Result<unsigned long> objectFileSize = getFileSize(objectPath); | |
| 273 | 1 | assert deleteFile(objectPath); | |
| 274 | 1 | assert objectFileSize.unwrap() == objectBytes.getSize(); | |
| 275 | |||
| 276 | // A successful compilation clears the error message of a previous one | ||
| 277 | 1 | Compiler wasmCompiler = Compiler(llvm::Triple("wasm32-unknown-unknown")); | |
| 278 | 1 | assert wasmCompiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 279 | 1 | assert !errorMessage.isEmpty(); | |
| 280 | 1 | assert !compiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 281 | 1 | assert errorMessage.isEmpty(); | |
| 282 | |||
| 283 | // Copies and assigned compilers own their TPDE compiler, so the original stays usable once they are disposed | ||
| 284 | 1 | const unsigned long objectSize = objectBytes.getSize(); | |
| 285 | 1 | assert compileWithTestCopy(compiler, module) == objectSize; | |
| 286 | 1 | wasmCompiler = compiler; | |
| 287 | 1 | assert wasmCompiler.isValid(); | |
| 288 | 1 | assert !wasmCompiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 289 | 1 | assert objectBytes.getSize() == objectSize; | |
| 290 | 1 | assert !compiler.compileToBuffer(module, objectBytes, errorMessage); | |
| 291 | 1 | assert objectBytes.getSize() == objectSize; | |
| 292 | 1 | return true; | |
| 293 | 1 | } | |
| 294 |