From c9b5324e6c890d813c1d17a834d8aca9b57e37e4 Mon Sep 17 00:00:00 2001 From: Inho Seo Date: Mon, 11 Jan 2021 10:44:01 +0000 Subject: [PATCH 01/18] Added a address sanitizer option, but need to check if it is turn on or not --- .../Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp | 9 ++++++++- .../compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index 33f1416789c7..4b62f165d84c 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -34,7 +34,7 @@ LLVMTargetOptions getDefaultLLVMTargetOptions() { { llvm::SubtargetFeatures features; llvm::StringMap hostFeatures; - if (llvm::sys::getHostCPUFeatures(hostFeatures)) { + if (llvm::sys::getHostCPUFeatures(hostFeatures)) { for (auto &feature : hostFeatures) { features.AddFeature(feature.first(), feature.second); } @@ -107,6 +107,13 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { llvm::cl::init(llvmTargetOptions.debugSymbols)); llvmTargetOptions.debugSymbols = clDebugSymbols; + static llvm::cl::opt clAddressSanitizer( + "iree-llvm-address-sanitizer", + llvm::cl::desc("Turn on address sanitization to detect memory violations"), + llvm::cl::init(llvmTargetOptions.addressSanitizer)); +// if(clAddressSanitizer) + llvmTargetOptions.addressSanitizer = clAddressSanitizer; + static llvm::cl::opt clLinkStatic( "iree-llvm-link-static", llvm::cl::desc( diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index 1ac53bc9ad06..441aa6ed4372 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -38,6 +38,7 @@ struct LLVMTargetOptions { // information is valid) it may significantly change the output program // and benchmarking bool debugSymbols = true; + bool addressSanitizer = false; // Link any required runtime libraries into the produced binaries statically. // This increases resulting binary size but enables the binaries to be used on From 9969eea01a06b08a6de634443a22d079ebbfa15d Mon Sep 17 00:00:00 2001 From: Inho Seo Date: Mon, 11 Jan 2021 23:05:44 +0000 Subject: [PATCH 02/18] Added assertion error for unimplemented address sanitization option --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index 467ebfdc195b..f38f77099397 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -66,6 +66,8 @@ std::unique_ptr createTargetMachine( LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, llvm::TargetMachine *machine, llvm::Module *module) { + if(options.addressSanitizer) + assert(false && "Address Sanitization feature has not been implemented yet."); llvm::LoopAnalysisManager loopAnalysisManager; llvm::FunctionAnalysisManager functionAnalysisManager; llvm::CGSCCAnalysisManager cGSCCAnalysisManager; From fdf59c9e3ee5f9ef11becd185c2a20977bc83882 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 11 Jan 2021 23:38:15 +0000 Subject: [PATCH 03/18] Try to fix failed checks --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index 4b62f165d84c..bbd48922e90b 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -34,7 +34,7 @@ LLVMTargetOptions getDefaultLLVMTargetOptions() { { llvm::SubtargetFeatures features; llvm::StringMap hostFeatures; - if (llvm::sys::getHostCPUFeatures(hostFeatures)) { + if (llvm::sys::getHostCPUFeatures(hostFeatures)) { for (auto &feature : hostFeatures) { features.AddFeature(feature.first(), feature.second); } @@ -111,7 +111,6 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { "iree-llvm-address-sanitizer", llvm::cl::desc("Turn on address sanitization to detect memory violations"), llvm::cl::init(llvmTargetOptions.addressSanitizer)); -// if(clAddressSanitizer) llvmTargetOptions.addressSanitizer = clAddressSanitizer; static llvm::cl::opt clLinkStatic( From 8884defa9390eb664ca3def6a2f17157b5e49ada Mon Sep 17 00:00:00 2001 From: inho9606 Date: Wed, 13 Jan 2021 04:41:39 +0000 Subject: [PATCH 04/18] Added a pass for address sanitization, but not tested yet --- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index f38f77099397..5de1477fc849 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -27,6 +27,8 @@ #include "llvm/Support/Host.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Instrumentation/AddressSanitizer.h" +// #include "clang/Basic/CodeGenOptions.h" namespace mlir { namespace iree_compiler { @@ -66,8 +68,6 @@ std::unique_ptr createTargetMachine( LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, llvm::TargetMachine *machine, llvm::Module *module) { - if(options.addressSanitizer) - assert(false && "Address Sanitization feature has not been implemented yet."); llvm::LoopAnalysisManager loopAnalysisManager; llvm::FunctionAnalysisManager functionAnalysisManager; llvm::CGSCCAnalysisManager cGSCCAnalysisManager; @@ -77,6 +77,7 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, llvm::StandardInstrumentations standardInstrumentations( /*DebugLogging=*/false); standardInstrumentations.registerCallbacks(passInstrumentationCallbacks); + llvm::ModulePassManager modulePassManager; llvm::PassBuilder passBuilder(false, machine, options.pipelineTuningOptions, {}, &passInstrumentationCallbacks); @@ -89,8 +90,27 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.registerLoopAnalyses(loopAnalysisManager); passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); + if (options.addressSanitizer) { + bool CompileKernel = false; + bool Recover = false; + bool UseAfterScope = true; + bool ModuleUseAfterScope = false; // asanUseGlobalsGC(TargetTriple, CodeGenOpts); + bool UseOdrIndicator = false; // CodeGenOpts.SanitizeAddressUseOdrIndicator; + passBuilder.registerOptimizerLastEPCallback( + [CompileKernel, Recover, UseAfterScope, ModuleUseAfterScope, + UseOdrIndicator](llvm::ModulePassManager &modulePassManager, + llvm::PassBuilder::OptimizationLevel Level) { + modulePassManager.addPass( + llvm::RequireAnalysisPass()); + modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(CompileKernel, Recover)); +// ModuleUseAfterScope, +// UseOdrIndicator)); + modulePassManager.addPass(createModuleToFunctionPassAdaptor( + llvm::AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); + }); + } + if (options.optLevel != llvm::PassBuilder::OptimizationLevel::O0) { - llvm::ModulePassManager modulePassManager; modulePassManager = passBuilder.buildPerModuleDefaultPipeline(options.optLevel); modulePassManager.run(*module, moduleAnalysisManager); From 6867e611aa3003318d5cfe40d7f3881858373c68 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Fri, 15 Jan 2021 04:30:48 +0000 Subject: [PATCH 05/18] Implemented address-sanitization option feature --- build_tools/cmake/iree_copts.cmake | 2 +- iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 4 ++++ iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/build_tools/cmake/iree_copts.cmake b/build_tools/cmake/iree_copts.cmake index 9c40cdb3e62f..6b3fbc0b9ac4 100644 --- a/build_tools/cmake/iree_copts.cmake +++ b/build_tools/cmake/iree_copts.cmake @@ -402,7 +402,7 @@ set(LLVM_ENABLE_IDE ON CACHE BOOL "" FORCE) # TODO(ataei): Use optional build time targets selection for LLVMAOT. set(LLVM_TARGETS_TO_BUILD "WebAssembly;X86;ARM;AArch64;RISCV" CACHE STRING "" FORCE) -set(LLVM_ENABLE_PROJECTS "mlir" CACHE STRING "" FORCE) +set(LLVM_ENABLE_PROJECTS "mlir;compiler-rt" CACHE STRING "" FORCE) set(LLVM_ENABLE_BINDINGS OFF CACHE BOOL "" FORCE) if(IREE_USE_LINKER) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index 7bb954ad7589..be5f48a3e664 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,6 +229,10 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } + if(options_.addressSanitizer) { + for(auto &function : llvmModule->getFunctionList()) + function.addFnAttr(llvm::Attribute::SanitizeAddress); + } // Try to grab a linker tool based on the options (and target environment). auto linkerTool = LinkerTool::getForTarget(targetTriple, options_); diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index 5de1477fc849..dc48288bc34e 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -77,7 +77,6 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, llvm::StandardInstrumentations standardInstrumentations( /*DebugLogging=*/false); standardInstrumentations.registerCallbacks(passInstrumentationCallbacks); - llvm::ModulePassManager modulePassManager; llvm::PassBuilder passBuilder(false, machine, options.pipelineTuningOptions, {}, &passInstrumentationCallbacks); @@ -102,15 +101,16 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, llvm::PassBuilder::OptimizationLevel Level) { modulePassManager.addPass( llvm::RequireAnalysisPass()); - modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(CompileKernel, Recover)); -// ModuleUseAfterScope, -// UseOdrIndicator)); + modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(CompileKernel, Recover, + ModuleUseAfterScope, + UseOdrIndicator)); modulePassManager.addPass(createModuleToFunctionPassAdaptor( llvm::AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); }); } if (options.optLevel != llvm::PassBuilder::OptimizationLevel::O0) { + llvm::ModulePassManager modulePassManager; modulePassManager = passBuilder.buildPerModuleDefaultPipeline(options.optLevel); modulePassManager.run(*module, moduleAnalysisManager); From 4ed1891352082dcd4aad66d2fb688bb3752df154 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 1 Feb 2021 09:29:17 +0000 Subject: [PATCH 06/18] Fixed formatting,, --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index be5f48a3e664..3ff5f40b4abe 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,7 +229,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if(options_.addressSanitizer) { + if (options_.addressSanitizer) { for(auto &function : llvmModule->getFunctionList()) function.addFnAttr(llvm::Attribute::SanitizeAddress); } From a2ed96010946c4da8ea2c0fd61762b5b83de3f64 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Thu, 4 Feb 2021 07:16:15 +0000 Subject: [PATCH 07/18] update to upstream --- .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 3 ++- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 26 +++++++++---------- .../HAL/Target/LLVM/LLVMTargetOptions.cpp | 14 +++++----- .../HAL/Target/LLVM/LLVMTargetOptions.h | 9 ++++++- .../local/loaders/legacy_library_loader.cc | 5 ++++ iree/schemas/dylib_executable_def.fbs | 6 +++++ 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index 3ff5f40b4abe..50128d8c2d2f 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,7 +229,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if (options_.addressSanitizer) { + if (options_.sanitizerKind == iree_Sanitizer_ADDRESS) { for(auto &function : llvmModule->getFunctionList()) function.addFnAttr(llvm::Attribute::SanitizeAddress); } @@ -344,6 +344,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, debugDatabaseFilenameRef); iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); + iree_DyLibExecutableDef_sanitized_kind_add(builder, options_.sanitizerKind); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index dc48288bc34e..63df39f43dec 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -28,7 +28,6 @@ #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" -// #include "clang/Basic/CodeGenOptions.h" namespace mlir { namespace iree_compiler { @@ -89,23 +88,24 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.registerLoopAnalyses(loopAnalysisManager); passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); - if (options.addressSanitizer) { - bool CompileKernel = false; - bool Recover = false; - bool UseAfterScope = true; - bool ModuleUseAfterScope = false; // asanUseGlobalsGC(TargetTriple, CodeGenOpts); - bool UseOdrIndicator = false; // CodeGenOpts.SanitizeAddressUseOdrIndicator; + + if (options.sanitizerKind == LLVMTargetOptions::Sanitizer::ADDRESS) { + bool compileKernel = false; + bool recover = false; + bool useAfterScope = true; + bool moduleUseAfterScope = false; + bool useOdrIndicator = false; passBuilder.registerOptimizerLastEPCallback( - [CompileKernel, Recover, UseAfterScope, ModuleUseAfterScope, - UseOdrIndicator](llvm::ModulePassManager &modulePassManager, + [compileKernel, recover, useAfterScope, moduleUseAfterScope, + useOdrIndicator](llvm::ModulePassManager &modulePassManager, llvm::PassBuilder::OptimizationLevel Level) { modulePassManager.addPass( llvm::RequireAnalysisPass()); - modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(CompileKernel, Recover, - ModuleUseAfterScope, - UseOdrIndicator)); + modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(compileKernel, recover, + moduleUseAfterScope, + useOdrIndicator)); modulePassManager.addPass(createModuleToFunctionPassAdaptor( - llvm::AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); + llvm::AddressSanitizerPass(compileKernel, recover, useAfterScope))); }); } diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index bbd48922e90b..77b1abf83b2a 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -82,6 +82,14 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { llvmTargetOptions.targetCPUFeatures = clTargetCPUFeatures; } + static llvm::cl::opt clSanitizerKind( + "iree-llvm-sanitize", + llvm::cl::desc( + "Specify LLVM sanitize feature such as 'address' ETC"), + llvm::cl::init("")); + if (clSanitizerKind == "address") + llvmTargetOptions.sanitizerKind = LLVMTargetOptions::Sanitizer::ADDRESS; + static llvm::cl::opt clTargetABI( "iree-llvm-target-abi", llvm::cl::desc("LLVM target machine ABI; specify for -mabi"), @@ -107,12 +115,6 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { llvm::cl::init(llvmTargetOptions.debugSymbols)); llvmTargetOptions.debugSymbols = clDebugSymbols; - static llvm::cl::opt clAddressSanitizer( - "iree-llvm-address-sanitizer", - llvm::cl::desc("Turn on address sanitization to detect memory violations"), - llvm::cl::init(llvmTargetOptions.addressSanitizer)); - llvmTargetOptions.addressSanitizer = clAddressSanitizer; - static llvm::cl::opt clLinkStatic( "iree-llvm-link-static", llvm::cl::desc( diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index 441aa6ed4372..63ee8e45220b 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -38,7 +38,14 @@ struct LLVMTargetOptions { // information is valid) it may significantly change the output program // and benchmarking bool debugSymbols = true; - bool addressSanitizer = false; + + // Define kinds of sanitizer. + enum Sanitizer { + NONE = 0, + ADDRESS = 1 + }; + + Sanitizer sanitizerKind = NONE; // Link any required runtime libraries into the produced binaries statically. // This increases resulting binary size but enables the binaries to be used on diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index 2a03f86ae9c0..0ac63567cdf6 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -81,6 +81,11 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( "executable library_embedded is missing/empty"); } + if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == iree_Sanitizer_ADDRESS && !__has_feature(address_sanitizer)) { + return iree_make_status(IREE_STATUS_UNAVAILABLE, + "Failed to load ASAN libraries"); + } + return iree_ok_status(); } diff --git a/iree/schemas/dylib_executable_def.fbs b/iree/schemas/dylib_executable_def.fbs index 8c3049330cee..2570bdded079 100644 --- a/iree/schemas/dylib_executable_def.fbs +++ b/iree/schemas/dylib_executable_def.fbs @@ -13,6 +13,11 @@ // limitations under the License. namespace iree; +// Define kinds of sanitizer used to connect CPU kernels and runtime +enum Sanitizer : ubyte { + NONE = 0, + ADDRESS +} // 'Dynamic Library (dylib) Executable'. @@ -30,6 +35,7 @@ table DyLibExecutableDef { debug_database_filename:string; debug_database_embedded:[ubyte]; + sanitized_kind:Sanitizer = NONE; // TODO(scotttodd): Relative file path from this flatbuffer file } From 14e8a6b87b7f5afa5e1d44843104dced6412d932 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Thu, 4 Feb 2021 11:26:26 +0000 Subject: [PATCH 08/18] Connected enum from schemas to LLVM Target Option" --- iree/compiler/Dialect/HAL/Target/LLVM/BUILD | 1 + .../Dialect/HAL/Target/LLVM/CMakeLists.txt | 1 + .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 4 ++-- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 15 ++++++++------- .../Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp | 5 ++--- .../Dialect/HAL/Target/LLVM/LLVMTargetOptions.h | 10 ++-------- iree/hal/local/loaders/legacy_library_loader.cc | 2 +- iree/schemas/dylib_executable_def.fbs | 6 +++--- 8 files changed, 20 insertions(+), 24 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/BUILD b/iree/compiler/Dialect/HAL/Target/LLVM/BUILD index 3224513f971e..d0b9da0b3467 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/BUILD +++ b/iree/compiler/Dialect/HAL/Target/LLVM/BUILD @@ -90,6 +90,7 @@ cc_library( "LLVMTargetOptions.h", ], deps = [ + "//iree/compiler/Utils", "@llvm-project//llvm:MC", "@llvm-project//llvm:Passes", "@llvm-project//llvm:Support", diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt b/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt index ed3cdd843110..a2ef671b0141 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt +++ b/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt @@ -81,6 +81,7 @@ iree_cc_library( LLVMPasses LLVMSupport LLVMTarget + iree::compiler::Utils PUBLIC ) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index 50128d8c2d2f..f4e5e8a8aa00 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,8 +229,8 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if (options_.sanitizerKind == iree_Sanitizer_ADDRESS) { - for(auto &function : llvmModule->getFunctionList()) + if (options_.sanitizerKind == iree_Sanitizer_kAddress) { + for (auto &function : llvmModule->getFunctionList()) function.addFnAttr(llvm::Attribute::SanitizeAddress); } diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index 63df39f43dec..d17aa2216432 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -89,7 +89,7 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); - if (options.sanitizerKind == LLVMTargetOptions::Sanitizer::ADDRESS) { + if (options.sanitizerKind == iree_Sanitizer_kAddress) { bool compileKernel = false; bool recover = false; bool useAfterScope = true; @@ -100,12 +100,13 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, useOdrIndicator](llvm::ModulePassManager &modulePassManager, llvm::PassBuilder::OptimizationLevel Level) { modulePassManager.addPass( - llvm::RequireAnalysisPass()); - modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(compileKernel, recover, - moduleUseAfterScope, - useOdrIndicator)); - modulePassManager.addPass(createModuleToFunctionPassAdaptor( - llvm::AddressSanitizerPass(compileKernel, recover, useAfterScope))); + llvm::RequireAnalysisPass()); + modulePassManager.addPass(llvm::ModuleAddressSanitizerPass( + compileKernel, recover, moduleUseAfterScope, useOdrIndicator)); + modulePassManager.addPass( + createModuleToFunctionPassAdaptor(llvm::AddressSanitizerPass( + compileKernel, recover, useAfterScope))); }); } diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index 77b1abf83b2a..bd5a2be60c28 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -84,11 +84,10 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { static llvm::cl::opt clSanitizerKind( "iree-llvm-sanitize", - llvm::cl::desc( - "Specify LLVM sanitize feature such as 'address' ETC"), + llvm::cl::desc("Specify LLVM sanitize feature such as 'address' ETC"), llvm::cl::init("")); if (clSanitizerKind == "address") - llvmTargetOptions.sanitizerKind = LLVMTargetOptions::Sanitizer::ADDRESS; + llvmTargetOptions.sanitizerKind = iree_Sanitizer_kAddress; static llvm::cl::opt clTargetABI( "iree-llvm-target-abi", diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index 63ee8e45220b..5eb7e42800eb 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -17,6 +17,7 @@ #include "llvm/Passes/PassBuilder.h" #include "llvm/Target/TargetOptions.h" +#include "iree/schemas/dylib_executable_def_builder.h" namespace mlir { namespace iree_compiler { @@ -38,14 +39,7 @@ struct LLVMTargetOptions { // information is valid) it may significantly change the output program // and benchmarking bool debugSymbols = true; - - // Define kinds of sanitizer. - enum Sanitizer { - NONE = 0, - ADDRESS = 1 - }; - - Sanitizer sanitizerKind = NONE; + iree_Sanitizer_enum_t sanitizerKind; // Link any required runtime libraries into the produced binaries statically. // This increases resulting binary size but enables the binaries to be used on diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index 0ac63567cdf6..e5d3ddcc9844 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -81,7 +81,7 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( "executable library_embedded is missing/empty"); } - if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == iree_Sanitizer_ADDRESS && !__has_feature(address_sanitizer)) { + if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == iree_Sanitizer_kAddress && !__has_feature(address_sanitizer)) { return iree_make_status(IREE_STATUS_UNAVAILABLE, "Failed to load ASAN libraries"); } diff --git a/iree/schemas/dylib_executable_def.fbs b/iree/schemas/dylib_executable_def.fbs index 2570bdded079..95e8ebacf2a4 100644 --- a/iree/schemas/dylib_executable_def.fbs +++ b/iree/schemas/dylib_executable_def.fbs @@ -15,8 +15,8 @@ namespace iree; // Define kinds of sanitizer used to connect CPU kernels and runtime enum Sanitizer : ubyte { - NONE = 0, - ADDRESS + kNone = 0, + kAddress } // 'Dynamic Library (dylib) Executable'. @@ -35,7 +35,7 @@ table DyLibExecutableDef { debug_database_filename:string; debug_database_embedded:[ubyte]; - sanitized_kind:Sanitizer = NONE; + sanitized_kind:Sanitizer = kNone; // TODO(scotttodd): Relative file path from this flatbuffer file } From 703ee68d0238373b56e4590368a112b153158623 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Thu, 4 Feb 2021 11:34:59 +0000 Subject: [PATCH 09/18] refixed iree_copts.cmake file --- build_tools/cmake/iree_copts.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_tools/cmake/iree_copts.cmake b/build_tools/cmake/iree_copts.cmake index 6b3fbc0b9ac4..9c40cdb3e62f 100644 --- a/build_tools/cmake/iree_copts.cmake +++ b/build_tools/cmake/iree_copts.cmake @@ -402,7 +402,7 @@ set(LLVM_ENABLE_IDE ON CACHE BOOL "" FORCE) # TODO(ataei): Use optional build time targets selection for LLVMAOT. set(LLVM_TARGETS_TO_BUILD "WebAssembly;X86;ARM;AArch64;RISCV" CACHE STRING "" FORCE) -set(LLVM_ENABLE_PROJECTS "mlir;compiler-rt" CACHE STRING "" FORCE) +set(LLVM_ENABLE_PROJECTS "mlir" CACHE STRING "" FORCE) set(LLVM_ENABLE_BINDINGS OFF CACHE BOOL "" FORCE) if(IREE_USE_LINKER) From f5b773dad7427110f9abd704d12e3ed022b51fae Mon Sep 17 00:00:00 2001 From: inho9606 Date: Thu, 4 Feb 2021 12:59:00 +0000 Subject: [PATCH 10/18] Try to fix clang-format --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h | 2 +- iree/hal/local/loaders/legacy_library_loader.cc | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index 5eb7e42800eb..5b0b7853d0a5 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -15,9 +15,9 @@ #ifndef IREE_COMPILER_DIALECT_HAL_TARGET_LLVM_LLVMTARGETOPTIONS_H_ #define IREE_COMPILER_DIALECT_HAL_TARGET_LLVM_LLVMTARGETOPTIONS_H_ +#include "iree/schemas/dylib_executable_def_builder.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Target/TargetOptions.h" -#include "iree/schemas/dylib_executable_def_builder.h" namespace mlir { namespace iree_compiler { diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index e5d3ddcc9844..445dccc74a1d 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -81,7 +81,9 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( "executable library_embedded is missing/empty"); } - if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == iree_Sanitizer_kAddress && !__has_feature(address_sanitizer)) { + if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == + iree_Sanitizer_kAddress && + !__has_feature(address_sanitizer)) { return iree_make_status(IREE_STATUS_UNAVAILABLE, "Failed to load ASAN libraries"); } From b5cfe2bad462a9bda0315714a7ed8eef3acc581c Mon Sep 17 00:00:00 2001 From: inho9606 Date: Fri, 5 Feb 2021 09:22:40 +0000 Subject: [PATCH 11/18] Added enum in LLVM TargetOptions, added error message on invalid argument for llvm-sanitize, changed code styls ETC --- .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 5 +-- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 2 +- .../HAL/Target/LLVM/LLVMTargetOptions.cpp | 7 ++++- .../HAL/Target/LLVM/LLVMTargetOptions.h | 7 +++-- .../local/loaders/legacy_library_loader.cc | 11 ++++--- iree/schemas/dylib_executable_def.fbs | 31 ++++++++++++------- 6 files changed, 41 insertions(+), 22 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index f4e5e8a8aa00..e4452f11c927 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,7 +229,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if (options_.sanitizerKind == iree_Sanitizer_kAddress) { + if (options_.sanitizerKind == LLVMTargetOptions::SanitizerKind::Address) { for (auto &function : llvmModule->getFunctionList()) function.addFnAttr(llvm::Attribute::SanitizeAddress); } @@ -344,7 +344,8 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, debugDatabaseFilenameRef); iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); - iree_DyLibExecutableDef_sanitized_kind_add(builder, options_.sanitizerKind); + iree_DyLibExecutableDef_sanitized_kind_add( + builder, (unsigned char)options_.sanitizerKind); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index d17aa2216432..31327162c1cc 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -89,7 +89,7 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); - if (options.sanitizerKind == iree_Sanitizer_kAddress) { + if (options.sanitizerKind == LLVMTargetOptions::SanitizerKind::Address) { bool compileKernel = false; bool recover = false; bool useAfterScope = true; diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index bd5a2be60c28..a244606f4874 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -87,7 +87,12 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { llvm::cl::desc("Specify LLVM sanitize feature such as 'address' ETC"), llvm::cl::init("")); if (clSanitizerKind == "address") - llvmTargetOptions.sanitizerKind = iree_Sanitizer_kAddress; + llvmTargetOptions.sanitizerKind = LLVMTargetOptions::SanitizerKind::Address; + else if (clSanitizerKind != "") { + llvm::errs() << "For now, you can use 'address' for llvm-sanitize, other " + "values are ignored\n"; + clSanitizerKind = ""; + } static llvm::cl::opt clTargetABI( "iree-llvm-target-abi", diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index 5b0b7853d0a5..a045485202a3 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -15,7 +15,6 @@ #ifndef IREE_COMPILER_DIALECT_HAL_TARGET_LLVM_LLVMTARGETOPTIONS_H_ #define IREE_COMPILER_DIALECT_HAL_TARGET_LLVM_LLVMTARGETOPTIONS_H_ -#include "iree/schemas/dylib_executable_def_builder.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Target/TargetOptions.h" @@ -39,7 +38,11 @@ struct LLVMTargetOptions { // information is valid) it may significantly change the output program // and benchmarking bool debugSymbols = true; - iree_Sanitizer_enum_t sanitizerKind; + + // Define and declare kinds of Sanitizer as enum + // the order in enum should be same as one in flat buffer schema + enum SanitizerKind { None = 0, Address }; + SanitizerKind sanitizerKind = SanitizerKind::None; // Link any required runtime libraries into the produced binaries statically. // This increases resulting binary size but enables the binaries to be used on diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index 445dccc74a1d..0de8b52357dc 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -17,6 +17,7 @@ #include "iree/base/dynamic_library.h" #include "iree/base/internal/file_io.h" #include "iree/base/internal/file_path.h" +#include "iree/base/target_platform.h" #include "iree/base/tracing.h" #include "iree/hal/local/local_executable.h" @@ -82,10 +83,12 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( } if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == - iree_Sanitizer_kAddress && - !__has_feature(address_sanitizer)) { - return iree_make_status(IREE_STATUS_UNAVAILABLE, - "Failed to load ASAN libraries"); + iree_Sanitizer_Address && + !IREE_SANITIZER_ADDRESS) { + return iree_make_status( + IREE_STATUS_UNAVAILABLE, + "Dynamic library executable is compiled with ASAN support, but this " + "host application failed to enable ASAN to load this executable"); } return iree_ok_status(); diff --git a/iree/schemas/dylib_executable_def.fbs b/iree/schemas/dylib_executable_def.fbs index 95e8ebacf2a4..a5c4f7b431a6 100644 --- a/iree/schemas/dylib_executable_def.fbs +++ b/iree/schemas/dylib_executable_def.fbs @@ -13,10 +13,11 @@ // limitations under the License. namespace iree; -// Define kinds of sanitizer used to connect CPU kernels and runtime +// Define kinds of sanitizers (the order in enum should be same as enum in LLVM +// TargetOptions) enum Sanitizer : ubyte { - kNone = 0, - kAddress + None = 0, + Address } // 'Dynamic Library (dylib) Executable'. @@ -26,16 +27,22 @@ file_extension "dlib"; // Dynamic library (.so/.dll/.dylib) executable module. table DyLibExecutableDef { - // A map of entry points to string names with the same order as in the executable op. - entry_points:[string]; - // An embedded (as opposed to external) dynamic library file. - // TODO(scotttodd): List of embedded files? - // TODO(scotttodd): Format of files, platform information (x86/arm/etc.) - library_embedded:[ubyte]; +// A map of entry points to string names with the same order as in the +// executable op. +entry_points: + [string]; +// An embedded (as opposed to external) dynamic library file. +// TODO(scotttodd): List of embedded files? +// TODO(scotttodd): Format of files, platform information (x86/arm/etc.) +library_embedded: + [ubyte]; - debug_database_filename:string; - debug_database_embedded:[ubyte]; - sanitized_kind:Sanitizer = kNone; +debug_database_filename: + string; +debug_database_embedded: + [ubyte]; +sanitized_kind: + Sanitizer = None; // TODO(scotttodd): Relative file path from this flatbuffer file } From d638722b41200a0e1d73785e6ce9810716195ea7 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Fri, 5 Feb 2021 09:27:45 +0000 Subject: [PATCH 12/18] Reverted BUILD and CMakeLists.txt --- iree/compiler/Dialect/HAL/Target/LLVM/BUILD | 1 - iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt | 1 - 2 files changed, 2 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/BUILD b/iree/compiler/Dialect/HAL/Target/LLVM/BUILD index d0b9da0b3467..3224513f971e 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/BUILD +++ b/iree/compiler/Dialect/HAL/Target/LLVM/BUILD @@ -90,7 +90,6 @@ cc_library( "LLVMTargetOptions.h", ], deps = [ - "//iree/compiler/Utils", "@llvm-project//llvm:MC", "@llvm-project//llvm:Passes", "@llvm-project//llvm:Support", diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt b/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt index a2ef671b0141..ed3cdd843110 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt +++ b/iree/compiler/Dialect/HAL/Target/LLVM/CMakeLists.txt @@ -81,7 +81,6 @@ iree_cc_library( LLVMPasses LLVMSupport LLVMTarget - iree::compiler::Utils PUBLIC ) From 55822a4388a422271b698efd15caf583a0880bab Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 8 Feb 2021 03:07:45 +0000 Subject: [PATCH 13/18] Changed llvm-sanitize input style, moved SanitizerKind enum to outside of option strucuture, and fixed style bug" --- .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 5 ++-- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 2 +- .../HAL/Target/LLVM/LLVMTargetOptions.cpp | 17 ++++------- .../HAL/Target/LLVM/LLVMTargetOptions.h | 13 +++++--- .../local/loaders/legacy_library_loader.cc | 18 ++++++----- iree/schemas/dylib_executable_def.fbs | 30 ++++++++----------- 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index e4452f11c927..cfc0a231ba1d 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,7 +229,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if (options_.sanitizerKind == LLVMTargetOptions::SanitizerKind::Address) { + if (options_.sanitizerKind == SanitizerKind::kAddress) { for (auto &function : llvmModule->getFunctionList()) function.addFnAttr(llvm::Attribute::SanitizeAddress); } @@ -344,8 +344,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, debugDatabaseFilenameRef); iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); - iree_DyLibExecutableDef_sanitized_kind_add( - builder, (unsigned char)options_.sanitizerKind); + iree_DyLibExecutableDef_sanitized_kind_add(builder, options_.sanitizerKind); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index 31327162c1cc..6c81c058804c 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -89,7 +89,7 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); - if (options.sanitizerKind == LLVMTargetOptions::SanitizerKind::Address) { + if (options.sanitizerKind == SanitizerKind::kAddress) { bool compileKernel = false; bool recover = false; bool useAfterScope = true; diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp index a244606f4874..b6c18e0904b9 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp @@ -82,17 +82,12 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() { llvmTargetOptions.targetCPUFeatures = clTargetCPUFeatures; } - static llvm::cl::opt clSanitizerKind( - "iree-llvm-sanitize", - llvm::cl::desc("Specify LLVM sanitize feature such as 'address' ETC"), - llvm::cl::init("")); - if (clSanitizerKind == "address") - llvmTargetOptions.sanitizerKind = LLVMTargetOptions::SanitizerKind::Address; - else if (clSanitizerKind != "") { - llvm::errs() << "For now, you can use 'address' for llvm-sanitize, other " - "values are ignored\n"; - clSanitizerKind = ""; - } + static llvm::cl::opt clSanitizerKind( + "iree-llvm-sanitize", llvm::cl::desc("Apply LLVM sanitize feature"), + llvm::cl::init(SanitizerKind::kNone), + llvm::cl::values(clEnumValN(SanitizerKind::kAddress, "address", + "Address sanitizer support"))); + llvmTargetOptions.sanitizerKind = clSanitizerKind; static llvm::cl::opt clTargetABI( "iree-llvm-target-abi", diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index a045485202a3..ec3565c70dfc 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -23,6 +23,13 @@ namespace iree_compiler { namespace IREE { namespace HAL { +// Defines kinds of Sanitizer as enum +// the order in enum should be same as one in flat buffer schema +enum SanitizerKind : unsigned char { + kNone = 0, + kAddress, +}; + struct LLVMTargetOptions { // Target machine configuration. std::string targetTriple; @@ -39,10 +46,8 @@ struct LLVMTargetOptions { // and benchmarking bool debugSymbols = true; - // Define and declare kinds of Sanitizer as enum - // the order in enum should be same as one in flat buffer schema - enum SanitizerKind { None = 0, Address }; - SanitizerKind sanitizerKind = SanitizerKind::None; + // Sanitizer Kind for CPU Kernels + SanitizerKind sanitizerKind = SanitizerKind::kNone; // Link any required runtime libraries into the produced binaries statically. // This increases resulting binary size but enables the binaries to be used on diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index 0de8b52357dc..47da6b64b770 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -82,13 +82,17 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( "executable library_embedded is missing/empty"); } - if (iree_DyLibExecutableDef_sanitized_kind_get(executable_def) == - iree_Sanitizer_Address && - !IREE_SANITIZER_ADDRESS) { - return iree_make_status( - IREE_STATUS_UNAVAILABLE, - "Dynamic library executable is compiled with ASAN support, but this " - "host application failed to enable ASAN to load this executable"); + switch (iree_DyLibExecutableDef_sanitized_kind_get(executable_def)) { + case iree_SanitizerKind_Address: + if (!IREE_SANITIZER_ADDRESS) + return iree_make_status( + IREE_STATUS_UNAVAILABLE, + "Dynamic library executable is compiled with ASAN support, but " + "this " + "host application failed to enable ASAN to load this executable"); + break; + default: + break; } return iree_ok_status(); diff --git a/iree/schemas/dylib_executable_def.fbs b/iree/schemas/dylib_executable_def.fbs index a5c4f7b431a6..38ee55b76145 100644 --- a/iree/schemas/dylib_executable_def.fbs +++ b/iree/schemas/dylib_executable_def.fbs @@ -13,9 +13,10 @@ // limitations under the License. namespace iree; + // Define kinds of sanitizers (the order in enum should be same as enum in LLVM // TargetOptions) -enum Sanitizer : ubyte { +enum SanitizerKind : ubyte { None = 0, Address } @@ -27,22 +28,17 @@ file_extension "dlib"; // Dynamic library (.so/.dll/.dylib) executable module. table DyLibExecutableDef { -// A map of entry points to string names with the same order as in the -// executable op. -entry_points: - [string]; -// An embedded (as opposed to external) dynamic library file. -// TODO(scotttodd): List of embedded files? -// TODO(scotttodd): Format of files, platform information (x86/arm/etc.) -library_embedded: - [ubyte]; - -debug_database_filename: - string; -debug_database_embedded: - [ubyte]; -sanitized_kind: - Sanitizer = None; + // A map of entry points to string names with the same order as in the + // executable op. + entry_points:[string]; + // An embedded (as opposed to external) dynamic library file. + // TODO(scotttodd): List of embedded files? + // TODO(scotttodd): Format of files, platform information (x86/arm/etc.) + library_embedded:[ubyte]; + + debug_database_filename:string; + debug_database_embedded:[ubyte]; + sanitized_kind:SanitizerKind = None; // TODO(scotttodd): Relative file path from this flatbuffer file } From 6c66cdda4540f1c89056432d7b024f7a9f408168 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 8 Feb 2021 03:31:21 +0000 Subject: [PATCH 14/18] Fixed enum class.. --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 2 +- iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index cfc0a231ba1d..a48162ff3e4c 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -344,7 +344,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, debugDatabaseFilenameRef); iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); - iree_DyLibExecutableDef_sanitized_kind_add(builder, options_.sanitizerKind); + iree_DyLibExecutableDef_sanitized_kind_add(builder, (unsigned char)options_.sanitizerKind); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index ec3565c70dfc..e2ca47676118 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -24,8 +24,8 @@ namespace IREE { namespace HAL { // Defines kinds of Sanitizer as enum -// the order in enum should be same as one in flat buffer schema -enum SanitizerKind : unsigned char { +// the order in enum class should be same as one in flat buffer schema +enum class SanitizerKind : unsigned char { kNone = 0, kAddress, }; From 38d47d6f436969eda7998af5b2d01e441fe265cd Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 8 Feb 2021 03:35:01 +0000 Subject: [PATCH 15/18] fixed clang-format.... --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index a48162ff3e4c..916c42f1d9f2 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -344,7 +344,8 @@ class LLVMAOTTargetBackend final : public TargetBackend { builder, debugDatabaseFilenameRef); iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); - iree_DyLibExecutableDef_sanitized_kind_add(builder, (unsigned char)options_.sanitizerKind); + iree_DyLibExecutableDef_sanitized_kind_add( + builder, (unsigned char)options_.sanitizerKind); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. From 2d2cd5f99a624caa32304edd0db7cbfff2bca5af Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 8 Feb 2021 03:42:52 +0000 Subject: [PATCH 16/18] efixed enum class type... final --- iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index e2ca47676118..fc9fa675f1c2 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -25,7 +25,7 @@ namespace HAL { // Defines kinds of Sanitizer as enum // the order in enum class should be same as one in flat buffer schema -enum class SanitizerKind : unsigned char { +enum class SanitizerKind { kNone = 0, kAddress, }; From c2a7855342b4a5d813b2f9ada15b42c74c315908 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Mon, 8 Feb 2021 11:09:05 +0000 Subject: [PATCH 17/18] moved variables in lambda and fixed some issues.. --- .../Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp | 13 ++++-- .../Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp | 41 ++++++++++--------- .../HAL/Target/LLVM/LLVMTargetOptions.h | 4 +- .../local/loaders/legacy_library_loader.cc | 8 ++-- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp index 916c42f1d9f2..d0dbde6b585f 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp @@ -229,9 +229,14 @@ class LLVMAOTTargetBackend final : public TargetBackend { return targetOp.emitError() << "failed to translate the MLIR LLVM " "dialect to the native llvm::Module"; } - if (options_.sanitizerKind == SanitizerKind::kAddress) { - for (auto &function : llvmModule->getFunctionList()) - function.addFnAttr(llvm::Attribute::SanitizeAddress); + + switch (options_.sanitizerKind) { + case SanitizerKind::kNone: + break; + case SanitizerKind::kAddress: { + for (auto &function : llvmModule->getFunctionList()) + function.addFnAttr(llvm::Attribute::SanitizeAddress); + } break; } // Try to grab a linker tool based on the options (and target environment). @@ -345,7 +350,7 @@ class LLVMAOTTargetBackend final : public TargetBackend { iree_DyLibExecutableDef_debug_database_embedded_add(builder, debugDatabaseRef); iree_DyLibExecutableDef_sanitized_kind_add( - builder, (unsigned char)options_.sanitizerKind); + builder, static_cast(options_.sanitizerKind)); iree_DyLibExecutableDef_end_as_root(builder); // Add the binary data to the target executable. diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp index 6c81c058804c..87c2f797d9ce 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp @@ -89,25 +89,28 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options, passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager, cGSCCAnalysisManager, moduleAnalysisManager); - if (options.sanitizerKind == SanitizerKind::kAddress) { - bool compileKernel = false; - bool recover = false; - bool useAfterScope = true; - bool moduleUseAfterScope = false; - bool useOdrIndicator = false; - passBuilder.registerOptimizerLastEPCallback( - [compileKernel, recover, useAfterScope, moduleUseAfterScope, - useOdrIndicator](llvm::ModulePassManager &modulePassManager, - llvm::PassBuilder::OptimizationLevel Level) { - modulePassManager.addPass( - llvm::RequireAnalysisPass()); - modulePassManager.addPass(llvm::ModuleAddressSanitizerPass( - compileKernel, recover, moduleUseAfterScope, useOdrIndicator)); - modulePassManager.addPass( - createModuleToFunctionPassAdaptor(llvm::AddressSanitizerPass( - compileKernel, recover, useAfterScope))); - }); + switch (options.sanitizerKind) { + case SanitizerKind::kNone: + break; + case SanitizerKind::kAddress: { + passBuilder.registerOptimizerLastEPCallback( + [](llvm::ModulePassManager &modulePassManager, + llvm::PassBuilder::OptimizationLevel Level) { + bool compileKernel = false; + bool recover = false; + bool useAfterScope = true; + bool moduleUseAfterScope = false; + bool useOdrIndicator = false; + modulePassManager.addPass( + llvm::RequireAnalysisPass()); + modulePassManager.addPass(llvm::ModuleAddressSanitizerPass( + compileKernel, recover, moduleUseAfterScope, useOdrIndicator)); + modulePassManager.addPass( + createModuleToFunctionPassAdaptor(llvm::AddressSanitizerPass( + compileKernel, recover, useAfterScope))); + }); + } break; } if (options.optLevel != llvm::PassBuilder::OptimizationLevel::O0) { diff --git a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h index fc9fa675f1c2..3e85ed34b9ad 100644 --- a/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h +++ b/iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h @@ -23,8 +23,8 @@ namespace iree_compiler { namespace IREE { namespace HAL { -// Defines kinds of Sanitizer as enum -// the order in enum class should be same as one in flat buffer schema +// Defines kinds of Sanitizer +// The order in enum class should be same as one in flat buffer schema enum class SanitizerKind { kNone = 0, kAddress, diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index 47da6b64b770..dd63e5b1239d 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -83,16 +83,16 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( } switch (iree_DyLibExecutableDef_sanitized_kind_get(executable_def)) { - case iree_SanitizerKind_Address: + case iree_SanitizerKind_None: + break; + case iree_SanitizerKind_Address: { if (!IREE_SANITIZER_ADDRESS) return iree_make_status( IREE_STATUS_UNAVAILABLE, "Dynamic library executable is compiled with ASAN support, but " "this " "host application failed to enable ASAN to load this executable"); - break; - default: - break; + } break; } return iree_ok_status(); From be37c3b2305e683dda8315534a386869df773f08 Mon Sep 17 00:00:00 2001 From: inho9606 Date: Tue, 9 Feb 2021 01:16:48 +0000 Subject: [PATCH 18/18] Fixed formatting.. --- iree/hal/local/loaders/legacy_library_loader.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iree/hal/local/loaders/legacy_library_loader.cc b/iree/hal/local/loaders/legacy_library_loader.cc index dd63e5b1239d..0b78f7b230a7 100644 --- a/iree/hal/local/loaders/legacy_library_loader.cc +++ b/iree/hal/local/loaders/legacy_library_loader.cc @@ -86,12 +86,13 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify( case iree_SanitizerKind_None: break; case iree_SanitizerKind_Address: { - if (!IREE_SANITIZER_ADDRESS) + if (!IREE_SANITIZER_ADDRESS) { return iree_make_status( IREE_STATUS_UNAVAILABLE, "Dynamic library executable is compiled with ASAN support, but " - "this " - "host application failed to enable ASAN to load this executable"); + "this host application failed to enable ASAN to load this " + "executable"); + } } break; }