Skip to content

Commit

Permalink
Add WASM executable format and produce it with LLVMAOTTarget.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottTodd committed Mar 12, 2021
1 parent 54749ce commit 98f2135
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion iree/compiler/Dialect/HAL/IR/HALBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<IREE::HAL::ExecutableFormat>($_self.getInt())";
Expand Down
26 changes: 22 additions & 4 deletions iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -269,10 +276,14 @@ class LLVMAOTTargetBackend final : public TargetBackend {
builder, static_cast<unsigned char>(options_.sanitizerKind));
iree_DyLibExecutableDef_end_as_root(builder);

uint32_t executableFormat =
targetTriple.isWasm()
? static_cast<uint32_t>(IREE::HAL::ExecutableFormat::WASM)
: static_cast<uint32_t>(IREE::HAL::ExecutableFormat::DyLib);

// Add the binary data to the target executable.
executableBuilder.create<IREE::HAL::ExecutableBinaryOp>(
targetOp.getLoc(), targetOp.sym_name(),
static_cast<uint32_t>(IREE::HAL::ExecutableFormat::DyLib),
targetOp.getLoc(), targetOp.sym_name(), executableFormat,
builder.getBufferAttr(executableBuilder.getContext()));
return success();
}
Expand All @@ -284,20 +295,27 @@ class LLVMAOTTargetBackend final : public TargetBackend {
void registerLLVMAOTTargetBackends(
std::function<LLVMTargetOptions()> 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<LLVMAOTTargetBackend>(queryOptions());
});
static TargetBackendRegistration wasmRegistration("wasm-llvm-aot", [=]() {
INIT_LLVM_TARGET(WebAssembly)
return std::make_unique<LLVMAOTTargetBackend>(queryOptions());
});

#undef INIT_LLVM_TARGET
}

} // namespace HAL
Expand Down

0 comments on commit 98f2135

Please sign in to comment.