From 98f2135ed300be57145c705ebe3a14b9641452ef Mon Sep 17 00:00:00 2001 From: Scott Todd Date: Wed, 10 Mar 2021 08:53:33 -0800 Subject: [PATCH] Add WASM executable format and produce it with LLVMAOTTarget. --- iree/compiler/Dialect/HAL/IR/HALBase.td | 4 ++- .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/iree/compiler/Dialect/HAL/IR/HALBase.td b/iree/compiler/Dialect/HAL/IR/HALBase.td index 7d2930231e1b..db0d962f39d8 100644 --- a/iree/compiler/Dialect/HAL/IR/HALBase.td +++ b/iree/compiler/Dialect/HAL/IR/HALBase.td @@ -238,6 +238,7 @@ def HAL_EF_Metal : I32EnumAttrCase<"Metal", 1297370181>; def HAL_EF_LLVM : I32EnumAttrCase<"LLVM", 1280071245>; def HAL_EF_DyLib : I32EnumAttrCase<"DyLib", 1145850178>; def HAL_EF_CUDA : I32EnumAttrCase<"CUDA", 1129661505>; +def HAL_EF_WASM : I32EnumAttrCase<"WASM", 1463898957>; def HAL_ExecutableFormatAttr : I32EnumAttr<"ExecutableFormat", "IREE HAL Executable format", [ HAL_EF_Unspecified, @@ -247,7 +248,8 @@ def HAL_ExecutableFormatAttr : HAL_EF_SpirV, HAL_EF_Metal, HAL_EF_DyLib, - HAL_EF_CUDA + HAL_EF_CUDA, + HAL_EF_WASM, ]> { let returnType = "IREE::HAL::ExecutableFormat"; let convertFromStorage = "static_cast($_self.getInt())"; diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index 76d25b548751..f0f02a40357a 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -71,7 +71,14 @@ class LLVMAOTTargetBackend final : public TargetBackend { // NOTE: we could vary these based on the options, such as by arch/etc. std::string name() const override { return "llvm_aot"; } - std::string filter_pattern() const override { return "dylib*"; } + std::string filter_pattern() const override { + llvm::Triple targetTriple(options_.targetTriple); + if (targetTriple.isWasm()) { + return "wasm*"; + } else { + return "dylib*"; + } + } void buildTranslationPassPipeline(OpPassManager &passManager) override { buildLLVMTransformPassPipeline(passManager); @@ -269,10 +276,14 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, static_cast(options_.sanitizerKind)); iree_DyLibExecutableDef_end_as_root(builder); + uint32_t executableFormat = + targetTriple.isWasm() + ? static_cast(IREE::HAL::ExecutableFormat::WASM) + : static_cast(IREE::HAL::ExecutableFormat::DyLib); + // Add the binary data to the target executable. executableBuilder.create( - targetOp.getLoc(), targetOp.sym_name(), - static_cast(IREE::HAL::ExecutableFormat::DyLib), + targetOp.getLoc(), targetOp.sym_name(), executableFormat, builder.getBufferAttr(executableBuilder.getContext())); return success(); } @@ -284,20 +295,27 @@ class LLVMAOTTargetBackend final : public TargetBackend { void registerLLVMAOTTargetBackends( std::function queryOptions) { getLLVMTargetOptionsFromFlags(); - static TargetBackendRegistration registration("dylib-llvm-aot", [=]() { + #define INIT_LLVM_TARGET(TargetName) \ LLVMInitialize##TargetName##Target(); \ LLVMInitialize##TargetName##TargetMC(); \ LLVMInitialize##TargetName##TargetInfo(); \ LLVMInitialize##TargetName##AsmPrinter(); \ LLVMInitialize##TargetName##AsmParser(); + + static TargetBackendRegistration dylibRegistration("dylib-llvm-aot", [=]() { INIT_LLVM_TARGET(X86) INIT_LLVM_TARGET(ARM) INIT_LLVM_TARGET(AArch64) INIT_LLVM_TARGET(RISCV) + return std::make_unique(queryOptions()); + }); + static TargetBackendRegistration wasmRegistration("wasm-llvm-aot", [=]() { INIT_LLVM_TARGET(WebAssembly) return std::make_unique(queryOptions()); }); + +#undef INIT_LLVM_TARGET } } // namespace HAL