From afa4cff5557bf198fe71a90178e14e6a41220c41 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Fri, 8 Nov 2024 03:10:06 -0500 Subject: [PATCH 1/9] Check the return value when mmapping a file and exit gently if failed Signed-off-by: Tung D. Le --- .../KrnlToLLVM/ConvertKrnlToLLVM.cpp | 20 ++++++--- src/Conversion/KrnlToLLVM/KrnlEntryPoint.cpp | 25 ----------- .../KrnlToLLVM/KrnlToLLVMHelper.cpp | 44 +++++++++++++++++++ .../KrnlToLLVM/KrnlToLLVMHelper.hpp | 10 +++++ src/Conversion/KrnlToLLVM/RuntimeAPI.cpp | 3 +- src/Runtime/OMExternalConstant.inc | 40 ++++++++++++++--- 6 files changed, 103 insertions(+), 39 deletions(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index 709ede3c34..7014f00aa0 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -612,15 +612,15 @@ void loadConstantsFromFile(ModuleOp &module, OpBuilder b(ctx); MultiDialectBuilder create(b, loc); + Type llvmI1Ty = IntegerType::get(ctx, 1); Type llvmI8Ty = IntegerType::get(ctx, 8); Type llvmI64Ty = IntegerType::get(ctx, 64); Type llvmI8PtrTy = getPointerType(ctx, llvmI8Ty); - Type llvmVoidTy = LLVM::LLVMVoidType::get(ctx); // The following function will be emitted inside the IR to load constants from // file. std::string loadAllConstantsFuncName = "omLoadConstantsFromFile"; - Type llvmFnType = LLVM::LLVMFunctionType::get(llvmVoidTy, {}, false); + Type llvmFnType = LLVM::LLVMFunctionType::get(llvmI1Ty, {}, false); // If calledByEntryPoint, this function will be called by entry points. // Otherwise, user program (C/C++/Java/Python) would call this function. @@ -629,6 +629,7 @@ void loadConstantsFromFile(ModuleOp &module, Operation *firstEntryPointOp = getFirstEntryOpInBlock(module, entryGlobalOps); assert(firstEntryPointOp && "No entry function exists"); + OpBuilder::InsertionGuard guard(b); b.setInsertionPoint(firstEntryPointOp); funcOp = create.llvm.func( loadAllConstantsFuncName, llvmFnType, /*createUniqueFunc=*/true); @@ -646,13 +647,16 @@ void loadConstantsFromFile(ModuleOp &module, std::find(entryName.begin(), entryName.end(), '\0'), entryName.end()); auto entryFunc = module.lookupSymbol(entryName); assert(entryFunc && "Entry function not found"); + OpBuilder::InsertionGuard guard(b); b.setInsertionPoint( &entryFunc.getBody().front(), entryFunc.getBody().front().begin()); FlatSymbolRefAttr loadAllConstantsRef = create.llvm.getOrInsertSymbolRef( module, LLVMBuilder::SymbolPostfix(module, loadAllConstantsFuncName), - llvmVoidTy, {}, + llvmI1Ty, {}, /*isVarArg=*/false); - create.llvm.call({}, loadAllConstantsRef, {}); + Value retVal = create.llvm.call({llvmI1Ty}, loadAllConstantsRef, {}); + equalOrFailed(module, b, loc, + create.llvm.constant(llvmI1Ty, static_cast(1)), retVal); } } else { OpBuilder::InsertionGuard guard(b); @@ -697,8 +701,11 @@ void loadConstantsFromFile(ModuleOp &module, // Call a function to mmap the binary file to memory. Value isleVal = create.llvm.constant(llvmI64Ty, isle); Value sizeVal = create.llvm.constant(llvmI64Ty, dataSize); - RuntimeAPI::callApi(b, loc, apiRegistry, RuntimeAPI::API::MMAP_BINARY_FILE, + Value retVal = RuntimeAPI::callApi(b, loc, apiRegistry, + RuntimeAPI::API::MMAP_BINARY_FILE, {packedGlobalPtr, fnameI8Ptr, sizeVal, isleVal}); + equalOrReturn(module, b, loc, + create.llvm.constant(llvmI1Ty, static_cast(1)), retVal, retVal); // Now set pointers for constants in the IR module->walk([&](LLVM::GlobalOp dataGlobalOp) -> WalkResult { @@ -725,11 +732,10 @@ void loadConstantsFromFile(ModuleOp &module, RuntimeAPI::callApi(b, loc, apiRegistry, RuntimeAPI::API::GET_EXTERNAL_CONSTANT_ADDR, {dataPtr, packedGlobalPtr, offsetVal}); - return WalkResult::advance(); }); - create.llvm._return(); + create.llvm._return(create.llvm.constant(llvmI1Ty, static_cast(1))); } //===----------------------------------------------------------------------===// diff --git a/src/Conversion/KrnlToLLVM/KrnlEntryPoint.cpp b/src/Conversion/KrnlToLLVM/KrnlEntryPoint.cpp index 91df014d69..3eec020df7 100644 --- a/src/Conversion/KrnlToLLVM/KrnlEntryPoint.cpp +++ b/src/Conversion/KrnlToLLVM/KrnlEntryPoint.cpp @@ -412,31 +412,6 @@ class KrnlEntryPointOpLowering : public OpRewritePattern { rewriter.getI64Type(), {rewriter.getI64Type()}); } - // Emit code for `IF lhs != rhs THEN return null ELSE do nothing` - void equalOrFailed(ModuleOp &module, PatternRewriter &rewriter, Location loc, - Value lhs, Value rhs, std::string errorMsg = "", - bool appendRHS = true) const { - MLIRContext *context = rewriter.getContext(); - MultiDialectBuilder create(rewriter, loc); - create.llvm.ifThenElse(/*cond=*/ - [&](const LLVMBuilder &createLLVM) { - return createLLVM.icmp(LLVM::ICmpPredicate::ne, lhs, rhs); - }, /*then=*/ - [&](const LLVMBuilder &createLLVM) { - MultiDialectBuilder create(createLLVM); - // Print an error message. - if (appendRHS) - create.krnl.printf( - StringRef(errorMsg), rhs, rewriter.getI64Type(), true); - else - create.krnl.printf(StringRef(errorMsg + "\n")); - // Set errno. - krnl::emitErrNo(module, rewriter, loc, EINVAL); - // Return NULL. - create.llvm._return(create.llvm.null(getI8PointerType(context))); - }); - } - void emitVerificationCodeForInputTensors(ModuleOp &module, PatternRewriter &rewriter, Location loc, const RuntimeAPIRegistry &apiRegistry, Value omTensorInputs, diff --git a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp index 64744cc690..444fec2a3f 100644 --- a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp +++ b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp @@ -24,6 +24,7 @@ #include "onnx-mlir/Compiler/OMCompilerRuntimeTypes.h" #include "src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.hpp" +#include "src/Dialect/Krnl/DialectBuilder.hpp" #include "src/Dialect/Krnl/KrnlOps.hpp" #include "src/Dialect/Mlir/DialectBuilder.hpp" #include "src/Dialect/ONNX/ONNXOps/OpHelper.hpp" @@ -342,5 +343,48 @@ bool isZOS(ModuleOp module) { return zOS; } +void equalOrFailed(ModuleOp &module, OpBuilder &rewriter, Location loc, + Value lhs, Value rhs, std::string errorMsg, bool appendRHS) { + MLIRContext *context = rewriter.getContext(); + MultiDialectBuilder create(rewriter, loc); + create.llvm.ifThenElse(/*cond=*/ + [&](const LLVMBuilder &createLLVM) { + return createLLVM.icmp(LLVM::ICmpPredicate::ne, lhs, rhs); + }, /*then=*/ + [&](const LLVMBuilder &createLLVM) { + MultiDialectBuilder create(createLLVM); + // Print an error message. + if (!errorMsg.empty()) { + if (appendRHS) + create.krnl.printf( + StringRef(errorMsg), rhs, rewriter.getI64Type(), true); + else + create.krnl.printf(StringRef(errorMsg + "\n")); + } + // Set errno. + emitErrNo(module, rewriter, loc, EINVAL); + // Return NULL. + create.llvm._return(create.llvm.null(getI8PointerType(context))); + }); +} + +void equalOrReturn(ModuleOp &module, OpBuilder &rewriter, Location loc, + Value lhs, Value rhs, Value retVal, std::string errorMsg) { + MLIRContext *context = rewriter.getContext(); + MultiDialectBuilder create(rewriter, loc); + create.llvm.ifThenElse(/*cond=*/ + [&](const LLVMBuilder &createLLVM) { + return createLLVM.icmp(LLVM::ICmpPredicate::ne, lhs, rhs); + }, /*then=*/ + [&](const LLVMBuilder &createLLVM) { + MultiDialectBuilder create(createLLVM); + // Print an error message. + if (!errorMsg.empty()) + create.krnl.printf(StringRef(errorMsg + "\n")); + // Return retVal. + create.llvm._return(retVal); + }); +} + } // namespace krnl } // namespace onnx_mlir diff --git a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.hpp b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.hpp index ef616d12f3..470209144f 100644 --- a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.hpp +++ b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.hpp @@ -69,6 +69,16 @@ std::string e2a_s(std::string e_s); void emitErrNo(mlir::ModuleOp module, mlir::OpBuilder &builder, mlir::Location loc, int err); +/// Emit code for `IF lhs != rhs THEN return null ELSE do nothing`. +void equalOrFailed(mlir::ModuleOp &module, mlir::OpBuilder &rewriter, + mlir::Location loc, mlir::Value lhs, mlir::Value rhs, + std::string errorMsg = "", bool appendRHS = true); + +/// Emit code for `IF lhs != rhs THEN return retVal ELSE do nothing`. +void equalOrReturn(mlir::ModuleOp &module, mlir::OpBuilder &rewriter, + mlir::Location loc, mlir::Value lhs, mlir::Value rhs, mlir::Value retVal, + std::string errorMsg = ""); + /// Creates an LLVM pointer type with the given element type and address space. /// This function is meant to be used in code supporting both typed and opaque /// pointers, as it will create an opaque pointer with the given address space diff --git a/src/Conversion/KrnlToLLVM/RuntimeAPI.cpp b/src/Conversion/KrnlToLLVM/RuntimeAPI.cpp index 5d810aa6eb..63146dbbba 100644 --- a/src/Conversion/KrnlToLLVM/RuntimeAPI.cpp +++ b/src/Conversion/KrnlToLLVM/RuntimeAPI.cpp @@ -64,6 +64,7 @@ RuntimeAPIRegistry::RuntimeAPIRegistry( : registry() { MLIRContext *context = module.getContext(); auto voidTy = LLVM::LLVMVoidType::get(context); + Type int1Ty = IntegerType::get(context, 1); auto int8Ty = IntegerType::get(context, 8); auto opaquePtrTy = onnx_mlir::krnl::getPointerType(context, int8Ty); auto opaquePtrPtrTy = onnx_mlir::krnl::getPointerType(context, opaquePtrTy); @@ -88,7 +89,7 @@ RuntimeAPIRegistry::RuntimeAPIRegistry( RuntimeAPI(API::GET_OMT_ARRAY, "omTensorListGetOmtArray", opaquePtrPtrTy, {opaquePtrTy}), RuntimeAPI(API::PRINT_OMTENSOR, "omTensorPrint", voidTy, {opaquePtrTy, opaquePtrTy}), RuntimeAPI(API::GET_OMTENSOR_LIST_SIZE, "omTensorListGetSize", int64Ty, {opaquePtrTy}), - RuntimeAPI(API::MMAP_BINARY_FILE, "omMMapBinaryFile", voidTy, {opaquePtrPtrTy, opaquePtrTy, int64Ty, int64Ty}), + RuntimeAPI(API::MMAP_BINARY_FILE, "omMMapBinaryFile", int1Ty, {opaquePtrPtrTy, opaquePtrTy, int64Ty, int64Ty}), RuntimeAPI(API::GET_EXTERNAL_CONSTANT_ADDR, "omGetExternalConstantAddr", voidTy, {opaquePtrPtrTy, opaquePtrPtrTy, int64Ty}), }; // clang-format on diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index c1b4e5e496..f8b3179385 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -19,6 +19,7 @@ typedef int make_iso_compilers_happy; #include #include +#include #include #include #include @@ -58,7 +59,7 @@ void checkEndianness(const char constPackIsLE) { /// /// This function is thread-safe. /// -void omMMapBinaryFile( +bool omMMapBinaryFile( void **constAddr, char *filename, int64_t size, int64_t isLE) { checkEndianness(isLE); char *fname = filename; @@ -67,7 +68,7 @@ void omMMapBinaryFile( char *tPath = strdup(fname); if (!tPath) { fprintf(stderr, "Error while strdup"); - return; + return false; } __a2e_s(tPath); fname = tPath; @@ -75,7 +76,10 @@ void omMMapBinaryFile( if (constAddr == NULL) { perror("Error: null pointer"); - return; +#ifdef __MVS__ + free(tPath); +#endif + return false; } if (constAddr[0] == NULL) { @@ -89,7 +93,10 @@ void omMMapBinaryFile( filePath = (char *)malloc(filePathLen); if (!filePath) { fprintf(stderr, "Error while malloc"); - return; +#ifdef __MVS__ + free(tPath); +#endif + return false; } memcpy(filePath, basePath, baseLen); memcpy(filePath + baseLen, DIR_SEPARATOR, sepLen); @@ -101,7 +108,12 @@ void omMMapBinaryFile( int fd = open(filePath, O_RDONLY); if (fd < 0) { fprintf(stderr, "Error while opening %s\n", filePath); - return; + if (basePath) + free(filePath); +#ifdef __MVS__ + free(tPath); +#endif + return false; } #ifdef __MVS__ @@ -113,7 +125,12 @@ void omMMapBinaryFile( if (tempAddr == MAP_FAILED) { fprintf(stderr, "Error while mmapping %s\n", fname); close(fd); - return; + if (basePath) + free(filePath); +#ifdef __MVS__ + free(tPath); +#endif + return false; } /* Prepare to compare-and-swap to setup the shared constAddr. @@ -135,11 +152,22 @@ void omMMapBinaryFile( close(fd); if (basePath) free(filePath); +#ifdef __MVS__ + free(tPath); +#endif + /* Make sure constAddr is the same as the mmap address. + */ + if (constAddr[0] != tempAddr) { + fprintf(stderr, "Error while updating the buffer address for constants\n"); + return false; + } + return true; } #ifdef __MVS__ free(tPath); #endif + return true; } /// Return the address of a constant at a given offset. From 03b00ca60a829b95ae6d9446b102fac9a8b38b1a Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Fri, 8 Nov 2024 07:48:54 -0500 Subject: [PATCH 2/9] Edit lit tests Signed-off-by: Tung D. Le --- .../big_endian/constants.mlir | 32 ++++++--- .../big_endian/symbol-postfix.mlir | 67 ++++++++++++------ .../litte_endian/constants.mlir | 33 ++++++--- .../litte_endian/symbol-postfix.mlir | 68 +++++++++++++------ 4 files changed, 142 insertions(+), 58 deletions(-) diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/constants.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/constants.mlir index 5c6417321f..48196c7c58 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/constants.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/constants.mlir @@ -39,7 +39,7 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK-LABEL: module // CHECK: llvm.func @omGetExternalConstantAddr(!llvm.ptr, !llvm.ptr, i64) -// CHECK: llvm.func @omMMapBinaryFile(!llvm.ptr, !llvm.ptr, i64, i64) +// CHECK: llvm.func @omMMapBinaryFile(!llvm.ptr, !llvm.ptr, i64, i64) -> i1 // CHECK: llvm.mlir.global internal constant @constant_2(dense<[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]> : tensor<10xi64>) {addr_space = 0 : i32, alignment = 4096 : i64} : !llvm.array<10 x i64> // CHECK: llvm.mlir.global internal @om_external_constant_data_constant_1() {addr_space = 0 : i32, alignment = 4096 : i64} : !llvm.ptr { // CHECK: [[VAR_0_4_:%.+]] = llvm.mlir.zero : !llvm.ptr @@ -59,22 +59,38 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK: llvm.return [[VAR_0_6_]] : !llvm.ptr // CHECK: } -// CHECK: llvm.func @omLoadConstantsFromFile() { +// CHECK: llvm.func @omLoadConstantsFromFile() -> i1 { // CHECK-DAG: [[VAR_0_9_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 // CHECK-DAG: [[VAR_1_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0 : !llvm.ptr -// CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.constant(0 : i64) : i64 -// CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr +// CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr +// CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-DAG: [[VAR_4_3_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 +// CHECK-DAG: [[VAR_5_3_:%.+]] = llvm.mlir.constant(0 : i64) : i64 // CHECK-DAG: [[VAR_6_3_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst : !llvm.ptr // CHECK-DAG: [[VAR_7_3_:%.+]] = llvm.mlir.addressof @om_external_constant_filename : !llvm.ptr -// CHECK: llvm.call @omMMapBinaryFile([[VAR_6_3_]], [[VAR_7_3_]], [[VAR_4_3_]], [[VAR_2_3_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> () -// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_3_3_]], [[VAR_6_3_]], [[VAR_2_3_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK: [[VAR_8_3_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_3_]], [[VAR_7_3_]], [[VAR_4_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 +// CHECK: [[VAR_9_3_:%.+]] = llvm.icmp "ne" [[VAR_3_3_]], [[VAR_8_3_]] : i1 +// CHECK: llvm.cond_br [[VAR_9_3_]], ^bb1, ^bb2 +// CHECK: ^bb1: // pred: ^bb0 +// CHECK: llvm.return [[VAR_8_3_]] : i1 +// CHECK: ^bb2: // pred: ^bb0 +// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_2_3_]], [[VAR_6_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK: llvm.call @omGetExternalConstantAddr([[VAR_1_3_]], [[VAR_6_3_]], [[VAR_0_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK: llvm.return +// CHECK: llvm.return [[VAR_3_3_]] : i1 // CHECK: } // CHECK: llvm.func @run_main_graph({{.*}}: !llvm.ptr) -> !llvm.ptr { -// CHECK: llvm.call @omLoadConstantsFromFile() : () -> () +// CHECK-DAG: [[VAR_3_4_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-DAG: [[VAR_4_4_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-DAG: [[VAR_5_4_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-DAG: [[VAR_6_4_:%.+]] = llvm.call @omLoadConstantsFromFile() : () -> i1 +// CHECK: [[VAR_7_4_:%.+]] = llvm.icmp "ne" [[VAR_5_4_]], [[VAR_6_4_]] : i1 +// CHECK: llvm.cond_br [[VAR_7_4_]], ^bb1, ^bb2 +// CHECK: ^bb1: // pred: ^bb0 +// CHECK: [[VAR_8_4_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK: llvm.store [[VAR_4_4_]], [[VAR_8_4_]] : i32, !llvm.ptr +// CHECK: llvm.return [[VAR_3_4_]] : !llvm.ptr +// CHECK: ^bb2: // pred: ^bb0 // CHECK: } } diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/symbol-postfix.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/symbol-postfix.mlir index d09bff4e12..884756676d 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/symbol-postfix.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/big_endian/symbol-postfix.mlir @@ -152,30 +152,57 @@ module attributes {"onnx-mlir.symbol-postfix" = "tag_constants_to_file"} { // CHECK-CONST-TO-FILE: llvm.return [[VAR_0_15_]] : !llvm.ptr // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile_tag_constants_to_file() { -// CHECK-CONST-TO-FILE-DAG: [[VAR_0_18_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_1_9_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_2_9_:%.+]] = llvm.mlir.constant(0 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_3_9_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_4_7_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_6_6_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_7_4_:%.+]] = llvm.mlir.addressof @om_external_constant_filename_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE: llvm.call @omMMapBinaryFile([[VAR_6_6_]], [[VAR_7_4_]], [[VAR_4_7_]], [[VAR_2_9_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> () -// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_3_9_]], [[VAR_6_6_]], [[VAR_2_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_1_9_]], [[VAR_6_6_]], [[VAR_0_18_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK-CONST-TO-FILE: llvm.return +// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile_tag_constants_to_file() -> i1 { +// CHECK-CONST-TO-FILE-DAG: [[VAR_0_19_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_1_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_2_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_10_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_4_9_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_5_9_:%.+]] = llvm.mlir.constant(0 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_6_8_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_7_5_:%.+]] = llvm.mlir.addressof @om_external_constant_filename_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE: [[VAR_8_4_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_8_]], [[VAR_7_5_]], [[VAR_4_9_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 +// CHECK-CONST-TO-FILE: [[VAR_9_4_:%.+]] = llvm.icmp "ne" [[VAR_3_10_]], [[VAR_8_4_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_9_4_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: llvm.return [[VAR_8_4_]] : i1 +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 +// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_2_10_]], [[VAR_6_8_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_1_10_]], [[VAR_6_8_]], [[VAR_0_19_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK-CONST-TO-FILE: llvm.return [[VAR_3_10_]] : i1 // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile() { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () -// CHECK-CONST-TO-FILE: llvm.return +// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile() -> i1 { +// CHECK-CONST-TO-FILE: [[VAR_0_20_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_20_]] : i1 // CHECK-CONST-TO-FILE: } // CHECK-CONST-TO-FILE: llvm.func @run_main_graph_tag_constants_to_file([[arg0_:%.+]]: !llvm.ptr) -> !llvm.ptr { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_11_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_4_10_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-CONST-TO-FILE-DAG: [[VAR_5_10_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_6_9_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: [[VAR_7_6_:%.+]] = llvm.icmp "ne" [[VAR_5_10_]], [[VAR_6_9_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_7_6_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_8_5_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.store [[VAR_4_10_]], [[VAR_8_5_]] : i32, !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_3_11_]] : !llvm.ptr +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @run_main_graph([[arg0_:%.+]]: !llvm.ptr) -> !llvm.ptr { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () -// CHECK-CONST-TO-FILE: [[VAR_0_20_:%.+]] = llvm.call @run_main_graph_tag_constants_to_file([[arg0_]]) : (!llvm.ptr) -> !llvm.ptr -// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_20_]] : !llvm.ptr + +// CHECK-CONST-TO-FILE: llvm.func @run_main_graph([[arg0_]]: !llvm.ptr) -> !llvm.ptr { +// CHECK-CONST-TO-FILE-DAG: [[VAR_0_22_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_1_12_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-CONST-TO-FILE-DAG: [[VAR_2_12_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_12_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: [[VAR_4_11_:%.+]] = llvm.icmp "ne" [[VAR_2_12_]], [[VAR_3_12_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_4_11_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_5_11_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.store [[VAR_1_12_]], [[VAR_5_11_]] : i32, !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_22_]] : !llvm.ptr +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_6_10_:%.+]] = llvm.call @run_main_graph_tag_constants_to_file([[arg0_]]) : (!llvm.ptr) -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_6_10_]] : !llvm.ptr // CHECK-CONST-TO-FILE: } } diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir index 919aa1990a..d6420b6022 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir @@ -38,7 +38,7 @@ func.func @test_constants_to_file() -> memref<10xi64> { return %2 : memref<10xi64> // CHECK: llvm.func @omGetExternalConstantAddr(!llvm.ptr, !llvm.ptr, i64) -// CHECK: llvm.func @omMMapBinaryFile(!llvm.ptr, !llvm.ptr, i64, i64) +// CHECK: llvm.func @omMMapBinaryFile(!llvm.ptr, !llvm.ptr, i64, i64) -> i1 // CHECK: llvm.mlir.global internal constant @constant_2(dense<[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]> : tensor<10xi64>) {addr_space = 0 : i32, alignment = 4096 : i64} : !llvm.array<10 x i64> // CHECK: llvm.mlir.global internal @om_external_constant_data_constant_1() {addr_space = 0 : i32, alignment = 4096 : i64} : !llvm.ptr { // CHECK: [[VAR_0_4_:%.+]] = llvm.mlir.zero : !llvm.ptr @@ -58,23 +58,38 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK: llvm.return [[VAR_0_6_]] : !llvm.ptr // CHECK: } -// CHECK: llvm.func @omLoadConstantsFromFile() { +// CHECK: llvm.func @omLoadConstantsFromFile() -> i1 { // CHECK-DAG: [[VAR_0_9_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 // CHECK-DAG: [[VAR_1_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0 : !llvm.ptr -// CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.constant(0 : i64) : i64 -// CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr +// CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr +// CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-DAG: [[VAR_4_3_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 -// CHECK-DAG: [[VAR_5_3_:%.+]] = llvm.mlir.constant(1 : i64) : i64 +// CHECK-DAG: [[VAR_5_3_:%.+]] = llvm.mlir.constant(0 : i64) : i64 // CHECK-DAG: [[VAR_6_3_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst : !llvm.ptr // CHECK-DAG: [[VAR_7_3_:%.+]] = llvm.mlir.addressof @om_external_constant_filename : !llvm.ptr -// CHECK: llvm.call @omMMapBinaryFile([[VAR_6_3_]], [[VAR_7_3_]], [[VAR_4_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> () -// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_3_3_]], [[VAR_6_3_]], [[VAR_2_3_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK: [[VAR_8_3_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_3_]], [[VAR_7_3_]], [[VAR_4_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 +// CHECK: [[VAR_9_3_:%.+]] = llvm.icmp "ne" [[VAR_3_3_]], [[VAR_8_3_]] : i1 +// CHECK: llvm.cond_br [[VAR_9_3_]], ^bb1, ^bb2 +// CHECK: ^bb1: // pred: ^bb0 +// CHECK: llvm.return [[VAR_8_3_]] : i1 +// CHECK: ^bb2: // pred: ^bb0 +// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_2_3_]], [[VAR_6_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK: llvm.call @omGetExternalConstantAddr([[VAR_1_3_]], [[VAR_6_3_]], [[VAR_0_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK: llvm.return +// CHECK: llvm.return [[VAR_3_3_]] : i1 // CHECK: } // CHECK: llvm.func @run_main_graph({{.*}}: !llvm.ptr) -> !llvm.ptr { -// CHECK: llvm.call @omLoadConstantsFromFile() : () -> () +// CHECK-DAG: [[VAR_3_4_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-DAG: [[VAR_4_4_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-DAG: [[VAR_5_4_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-DAG: [[VAR_6_4_:%.+]] = llvm.call @omLoadConstantsFromFile() : () -> i1 +// CHECK: [[VAR_7_4_:%.+]] = llvm.icmp "ne" [[VAR_5_4_]], [[VAR_6_4_]] : i1 +// CHECK: llvm.cond_br [[VAR_7_4_]], ^bb1, ^bb2 +// CHECK: ^bb1: // pred: ^bb0 +// CHECK: [[VAR_8_4_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK: llvm.store [[VAR_4_4_]], [[VAR_8_4_]] : i32, !llvm.ptr +// CHECK: llvm.return [[VAR_3_4_]] : !llvm.ptr +// CHECK: ^bb2: // pred: ^bb0 // CHECK: } } diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir index 6414e5722b..faf0613539 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir @@ -152,31 +152,57 @@ module attributes {"onnx-mlir.symbol-postfix" = "tag_constants_to_file"} { // CHECK-CONST-TO-FILE: llvm.return [[VAR_0_15_]] : !llvm.ptr // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile_tag_constants_to_file() { -// CHECK-CONST-TO-FILE-DAG: [[VAR_0_18_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_1_9_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_2_9_:%.+]] = llvm.mlir.constant(0 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_3_9_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_4_7_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_5_8_:%.+]] = llvm.mlir.constant(1 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_6_6_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE-DAG: [[VAR_7_4_:%.+]] = llvm.mlir.addressof @om_external_constant_filename_tag_constants_to_file : !llvm.ptr -// CHECK-CONST-TO-FILE: llvm.call @omMMapBinaryFile([[VAR_6_6_]], [[VAR_7_4_]], [[VAR_4_7_]], [[VAR_5_8_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> () -// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_3_9_]], [[VAR_6_6_]], [[VAR_2_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_1_9_]], [[VAR_6_6_]], [[VAR_0_18_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () -// CHECK-CONST-TO-FILE: llvm.return +// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile_tag_constants_to_file() -> i1 { +// CHECK-CONST-TO-FILE-DAG: [[VAR_0_19_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_1_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_2_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_10_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_4_9_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_5_9_:%.+]] = llvm.mlir.constant(0 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_6_8_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_7_5_:%.+]] = llvm.mlir.addressof @om_external_constant_filename_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE: [[VAR_8_4_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_8_]], [[VAR_7_5_]], [[VAR_4_9_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 +// CHECK-CONST-TO-FILE: [[VAR_9_4_:%.+]] = llvm.icmp "ne" [[VAR_3_10_]], [[VAR_8_4_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_9_4_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: llvm.return [[VAR_8_4_]] : i1 +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 +// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_2_10_]], [[VAR_6_8_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_1_10_]], [[VAR_6_8_]], [[VAR_0_19_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK-CONST-TO-FILE: llvm.return [[VAR_3_10_]] : i1 // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile() { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () -// CHECK-CONST-TO-FILE: llvm.return +// CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile() -> i1 { +// CHECK-CONST-TO-FILE: [[VAR_0_20_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_20_]] : i1 // CHECK-CONST-TO-FILE: } // CHECK-CONST-TO-FILE: llvm.func @run_main_graph_tag_constants_to_file([[arg0_:%.+]]: !llvm.ptr) -> !llvm.ptr { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_11_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_4_10_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-CONST-TO-FILE-DAG: [[VAR_5_10_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_6_9_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: [[VAR_7_6_:%.+]] = llvm.icmp "ne" [[VAR_5_10_]], [[VAR_6_9_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_7_6_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_8_5_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.store [[VAR_4_10_]], [[VAR_8_5_]] : i32, !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_3_11_]] : !llvm.ptr +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 // CHECK-CONST-TO-FILE: } -// CHECK-CONST-TO-FILE: llvm.func @run_main_graph([[arg0_:%.+]]: !llvm.ptr) -> !llvm.ptr { -// CHECK-CONST-TO-FILE: llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> () -// CHECK-CONST-TO-FILE: [[VAR_0_20_:%.+]] = llvm.call @run_main_graph_tag_constants_to_file([[arg0_]]) : (!llvm.ptr) -> !llvm.ptr -// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_20_]] : !llvm.ptr + +// CHECK-CONST-TO-FILE: llvm.func @run_main_graph([[arg0_]]: !llvm.ptr) -> !llvm.ptr { +// CHECK-CONST-TO-FILE-DAG: [[VAR_0_22_:%.+]] = llvm.mlir.zero : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_1_12_:%.+]] = llvm.mlir.constant(22 : i32) : i32 +// CHECK-CONST-TO-FILE-DAG: [[VAR_2_12_:%.+]] = llvm.mlir.constant(true) : i1 +// CHECK-CONST-TO-FILE-DAG: [[VAR_3_12_:%.+]] = llvm.call @omLoadConstantsFromFile_tag_constants_to_file() : () -> i1 +// CHECK-CONST-TO-FILE: [[VAR_4_11_:%.+]] = llvm.icmp "ne" [[VAR_2_12_]], [[VAR_3_12_]] : i1 +// CHECK-CONST-TO-FILE: llvm.cond_br [[VAR_4_11_]], ^bb1, ^bb2 +// CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_5_11_:%.+]] = llvm.call @__errno_location() : () -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.store [[VAR_1_12_]], [[VAR_5_11_]] : i32, !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_0_22_]] : !llvm.ptr +// CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 +// CHECK-CONST-TO-FILE: [[VAR_6_10_:%.+]] = llvm.call @run_main_graph_tag_constants_to_file([[arg0_]]) : (!llvm.ptr) -> !llvm.ptr +// CHECK-CONST-TO-FILE: llvm.return [[VAR_6_10_]] : !llvm.ptr // CHECK-CONST-TO-FILE: } } From 79b9b952b7b49d3816fbd2f85caed1143ed5217e Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Fri, 8 Nov 2024 08:44:28 -0500 Subject: [PATCH 3/9] Fix isLE value in lit tests for little endian machines Signed-off-by: Tung D. Le --- .../krnl_to_llvm/constants_to_file/litte_endian/constants.mlir | 2 +- .../constants_to_file/litte_endian/symbol-postfix.mlir | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir index d6420b6022..6233c4e87c 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir @@ -64,7 +64,7 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr // CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-DAG: [[VAR_4_3_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 -// CHECK-DAG: [[VAR_5_3_:%.+]] = llvm.mlir.constant(0 : i64) : i64 +// CHECK-DAG: [[VAR_5_3_:%.+]] = llvm.mlir.constant(1 : i64) : i64 // CHECK-DAG: [[VAR_6_3_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst : !llvm.ptr // CHECK-DAG: [[VAR_7_3_:%.+]] = llvm.mlir.addressof @om_external_constant_filename : !llvm.ptr // CHECK: [[VAR_8_3_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_3_]], [[VAR_7_3_]], [[VAR_4_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir index faf0613539..0726a94c2b 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir @@ -158,7 +158,7 @@ module attributes {"onnx-mlir.symbol-postfix" = "tag_constants_to_file"} { // CHECK-CONST-TO-FILE-DAG: [[VAR_2_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr // CHECK-CONST-TO-FILE-DAG: [[VAR_3_10_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-CONST-TO-FILE-DAG: [[VAR_4_9_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 -// CHECK-CONST-TO-FILE-DAG: [[VAR_5_9_:%.+]] = llvm.mlir.constant(0 : i64) : i64 +// CHECK-CONST-TO-FILE-DAG: [[VAR_5_9_:%.+]] = llvm.mlir.constant(1 : i64) : i64 // CHECK-CONST-TO-FILE-DAG: [[VAR_6_8_:%.+]] = llvm.mlir.addressof @om_external_constant_packedConst_tag_constants_to_file : !llvm.ptr // CHECK-CONST-TO-FILE-DAG: [[VAR_7_5_:%.+]] = llvm.mlir.addressof @om_external_constant_filename_tag_constants_to_file : !llvm.ptr // CHECK-CONST-TO-FILE: [[VAR_8_4_:%.+]] = llvm.call @omMMapBinaryFile([[VAR_6_8_]], [[VAR_7_5_]], [[VAR_4_9_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64, i64) -> i1 From 7f8d1e079482893d34dcb3478ba908e32c77c9db Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Fri, 8 Nov 2024 09:02:44 -0500 Subject: [PATCH 4/9] Fix offset value in lit tests for little endian machines Signed-off-by: Tung D. Le --- .../krnl_to_llvm/constants_to_file/litte_endian/constants.mlir | 3 ++- .../constants_to_file/litte_endian/symbol-postfix.mlir | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir index 6233c4e87c..00a3183d99 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/constants.mlir @@ -61,6 +61,7 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK: llvm.func @omLoadConstantsFromFile() -> i1 { // CHECK-DAG: [[VAR_0_9_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 // CHECK-DAG: [[VAR_1_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0 : !llvm.ptr +// CHECK-DAG: [[VAR_1_4_:%.+]] = llvm.mlir.constant(0 : i64) : i64 // CHECK-DAG: [[VAR_2_3_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1 : !llvm.ptr // CHECK-DAG: [[VAR_3_3_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-DAG: [[VAR_4_3_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 @@ -73,7 +74,7 @@ func.func @test_constants_to_file() -> memref<10xi64> { // CHECK: ^bb1: // pred: ^bb0 // CHECK: llvm.return [[VAR_8_3_]] : i1 // CHECK: ^bb2: // pred: ^bb0 -// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_2_3_]], [[VAR_6_3_]], [[VAR_5_3_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK: llvm.call @omGetExternalConstantAddr([[VAR_2_3_]], [[VAR_6_3_]], [[VAR_1_4_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK: llvm.call @omGetExternalConstantAddr([[VAR_1_3_]], [[VAR_6_3_]], [[VAR_0_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK: llvm.return [[VAR_3_3_]] : i1 // CHECK: } diff --git a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir index 0726a94c2b..f763b6b3ba 100644 --- a/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir +++ b/test/mlir/conversion/krnl_to_llvm/constants_to_file/litte_endian/symbol-postfix.mlir @@ -155,6 +155,7 @@ module attributes {"onnx-mlir.symbol-postfix" = "tag_constants_to_file"} { // CHECK-CONST-TO-FILE: llvm.func @omLoadConstantsFromFile_tag_constants_to_file() -> i1 { // CHECK-CONST-TO-FILE-DAG: [[VAR_0_19_:%.+]] = llvm.mlir.constant(4096 : i64) : i64 // CHECK-CONST-TO-FILE-DAG: [[VAR_1_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_0_tag_constants_to_file : !llvm.ptr +// CHECK-CONST-TO-FILE-DAG: [[VAR_1_11_:%.+]] = llvm.mlir.constant(0 : i64) : i64 // CHECK-CONST-TO-FILE-DAG: [[VAR_2_10_:%.+]] = llvm.mlir.addressof @om_external_constant_data_constant_1_tag_constants_to_file : !llvm.ptr // CHECK-CONST-TO-FILE-DAG: [[VAR_3_10_:%.+]] = llvm.mlir.constant(true) : i1 // CHECK-CONST-TO-FILE-DAG: [[VAR_4_9_:%.+]] = llvm.mlir.constant(4176 : i64) : i64 @@ -167,7 +168,7 @@ module attributes {"onnx-mlir.symbol-postfix" = "tag_constants_to_file"} { // CHECK-CONST-TO-FILE: ^bb1: // pred: ^bb0 // CHECK-CONST-TO-FILE: llvm.return [[VAR_8_4_]] : i1 // CHECK-CONST-TO-FILE: ^bb2: // pred: ^bb0 -// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_2_10_]], [[VAR_6_8_]], [[VAR_5_9_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () +// CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_2_10_]], [[VAR_6_8_]], [[VAR_1_11_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK-CONST-TO-FILE: llvm.call @omGetExternalConstantAddr([[VAR_1_10_]], [[VAR_6_8_]], [[VAR_0_19_]]) : (!llvm.ptr, !llvm.ptr, i64) -> () // CHECK-CONST-TO-FILE: llvm.return [[VAR_3_10_]] : i1 // CHECK-CONST-TO-FILE: } From c0b0a4df95c5c16ce1c112f1eb50c52a63198b48 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Sun, 10 Nov 2024 20:42:19 -0500 Subject: [PATCH 5/9] Fix a bug in z/OS code Signed-off-by: Tung D. Le --- src/Runtime/OMExternalConstant.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index f8b3179385..a69b421a95 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -138,7 +138,7 @@ bool omMMapBinaryFile( */ #ifdef __MVS__ void *expected = NULL; - if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)tempAddr)) + if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) munmap(tempAddr, size); #else if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) From 81564522f1b93f27de52f451500498530b0511b4 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Mon, 11 Nov 2024 20:59:30 -0500 Subject: [PATCH 6/9] Requested changes Signed-off-by: Tung D. Le --- src/Runtime/OMExternalConstant.inc | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index a69b421a95..7eea41db7c 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -43,7 +43,7 @@ const int i = 1; void checkEndianness(const char constPackIsLE) { if (XOR(IS_SYSTEM_LE(), constPackIsLE)) { fprintf(stderr, "Constant pack is stored in a byte order that is not " - "native to this current system."); + "native to this current system: %s", strerror(errno)); exit(1); } } @@ -67,7 +67,7 @@ bool omMMapBinaryFile( // Convert the file name to EBCDIC for the open call. char *tPath = strdup(fname); if (!tPath) { - fprintf(stderr, "Error while strdup"); + fprintf(stderr, "Error while strdup: %s", strerror(errno)); return false; } __a2e_s(tPath); @@ -75,7 +75,7 @@ bool omMMapBinaryFile( #endif if (constAddr == NULL) { - perror("Error: null pointer"); + fprintf(stderr, "Error: null pointer: %s", strerror(errno)); #ifdef __MVS__ free(tPath); #endif @@ -89,25 +89,22 @@ bool omMMapBinaryFile( size_t baseLen = strlen(basePath); size_t fnameLen = strlen(fname); size_t sepLen = strlen(DIR_SEPARATOR); - size_t filePathLen = baseLen + sepLen + fnameLen; + size_t filePathLen = baseLen + sepLen + fnameLen + 1; filePath = (char *)malloc(filePathLen); if (!filePath) { - fprintf(stderr, "Error while malloc"); + fprintf(stderr, "Error while malloc: %s", strerror(errno)); #ifdef __MVS__ free(tPath); #endif return false; } - memcpy(filePath, basePath, baseLen); - memcpy(filePath + baseLen, DIR_SEPARATOR, sepLen); - memcpy(filePath + baseLen + sepLen, fname, fnameLen); - filePath[filePathLen] = '\0'; + snprintf(filePath, filePathLen, "%s%s%s", basePath, DIR_SEPARATOR, fname); } else { filePath = (char *)fname; } int fd = open(filePath, O_RDONLY); if (fd < 0) { - fprintf(stderr, "Error while opening %s\n", filePath); + fprintf(stderr, "Error while opening %s: %s\n", filePath, strerror(errno)); if (basePath) free(filePath); #ifdef __MVS__ @@ -123,7 +120,7 @@ bool omMMapBinaryFile( #endif if (tempAddr == MAP_FAILED) { - fprintf(stderr, "Error while mmapping %s\n", fname); + fprintf(stderr, "Error while mmapping %s: %s\n", fname, strerror(errno)); close(fd); if (basePath) free(filePath); @@ -158,7 +155,9 @@ bool omMMapBinaryFile( /* Make sure constAddr is the same as the mmap address. */ if (constAddr[0] != tempAddr) { - fprintf(stderr, "Error while updating the buffer address for constants\n"); + fprintf(stderr, + "Error while updating the buffer address for constants: %s\n", + strerror(errno)); return false; } return true; @@ -181,11 +180,11 @@ bool omMMapBinaryFile( void omGetExternalConstantAddr( void **outputAddr, void **baseAddr, int64_t offset) { if (outputAddr == NULL) { - perror("Error: null pointer"); + fprintf(stderr, "Error: null pointer: %s", strerror(errno)); return; } if (baseAddr == NULL) { - perror("Error: null pointer"); + fprintf(stderr, "Error: null pointer: %s", strerror(errno)); return; } // Constant is already loaded. Nothing to do. From d0f8bcbad565da34e48d1026766b1f8dd05d9620 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Tue, 12 Nov 2024 00:01:32 -0500 Subject: [PATCH 7/9] Requested changes Signed-off-by: Tung D. Le --- src/Runtime/OMExternalConstant.inc | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index 7eea41db7c..86e304a713 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -43,7 +43,7 @@ const int i = 1; void checkEndianness(const char constPackIsLE) { if (XOR(IS_SYSTEM_LE(), constPackIsLE)) { fprintf(stderr, "Constant pack is stored in a byte order that is not " - "native to this current system: %s", strerror(errno)); + "native to this current system."); exit(1); } } @@ -75,7 +75,7 @@ bool omMMapBinaryFile( #endif if (constAddr == NULL) { - fprintf(stderr, "Error: null pointer: %s", strerror(errno)); + fprintf(stderr, "Error: null pointer."); #ifdef __MVS__ free(tPath); #endif @@ -135,11 +135,15 @@ bool omMMapBinaryFile( */ #ifdef __MVS__ void *expected = NULL; - if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) + if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) { munmap(tempAddr, size); + tempAddr = NULL; + } #else - if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) + if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) { munmap(tempAddr, size); + tempAddr = NULL; + } #endif /* Either we succeeded in setting constAddr or someone else did it. @@ -154,10 +158,9 @@ bool omMMapBinaryFile( #endif /* Make sure constAddr is the same as the mmap address. */ - if (constAddr[0] != tempAddr) { + if (tempAddr && (constAddr[0] != tempAddr)) { fprintf(stderr, - "Error while updating the buffer address for constants: %s\n", - strerror(errno)); + "Error while updating the buffer address for constants.\n"); return false; } return true; @@ -180,11 +183,11 @@ bool omMMapBinaryFile( void omGetExternalConstantAddr( void **outputAddr, void **baseAddr, int64_t offset) { if (outputAddr == NULL) { - fprintf(stderr, "Error: null pointer: %s", strerror(errno)); + fprintf(stderr, "Error: null pointer."); return; } if (baseAddr == NULL) { - fprintf(stderr, "Error: null pointer: %s", strerror(errno)); + fprintf(stderr, "Error: null pointer."); return; } // Constant is already loaded. Nothing to do. From 1d93a7df4b2fd4faaa025d8a69c1f7e9a1dd0651 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Tue, 12 Nov 2024 00:26:02 -0500 Subject: [PATCH 8/9] Remove the check Signed-off-by: Tung D. Le --- src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp | 1 - src/Runtime/OMExternalConstant.inc | 15 ++------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp index 444fec2a3f..a3f12a59c5 100644 --- a/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp +++ b/src/Conversion/KrnlToLLVM/KrnlToLLVMHelper.cpp @@ -370,7 +370,6 @@ void equalOrFailed(ModuleOp &module, OpBuilder &rewriter, Location loc, void equalOrReturn(ModuleOp &module, OpBuilder &rewriter, Location loc, Value lhs, Value rhs, Value retVal, std::string errorMsg) { - MLIRContext *context = rewriter.getContext(); MultiDialectBuilder create(rewriter, loc); create.llvm.ifThenElse(/*cond=*/ [&](const LLVMBuilder &createLLVM) { diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index 86e304a713..769924e788 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -135,15 +135,11 @@ bool omMMapBinaryFile( */ #ifdef __MVS__ void *expected = NULL; - if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) { + if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) munmap(tempAddr, size); - tempAddr = NULL; - } #else - if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) { + if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) munmap(tempAddr, size); - tempAddr = NULL; - } #endif /* Either we succeeded in setting constAddr or someone else did it. @@ -156,13 +152,6 @@ bool omMMapBinaryFile( #ifdef __MVS__ free(tPath); #endif - /* Make sure constAddr is the same as the mmap address. - */ - if (tempAddr && (constAddr[0] != tempAddr)) { - fprintf(stderr, - "Error while updating the buffer address for constants.\n"); - return false; - } return true; } From 1042130826af6201efc7e158b982c8c21914ccf7 Mon Sep 17 00:00:00 2001 From: "Tung D. Le" Date: Wed, 13 Nov 2024 20:33:45 -0500 Subject: [PATCH 9/9] Convert filename to EBCDIC when generating code for zOS Signed-off-by: Tung D. Le --- .../KrnlToLLVM/ConvertKrnlToLLVM.cpp | 1 + src/Runtime/OMExternalConstant.inc | 123 +++++++----------- 2 files changed, 47 insertions(+), 77 deletions(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index 7014f00aa0..3f69e8be9d 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -569,6 +569,7 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, OpBuilder::InsertionGuard guard(b); b.setInsertionPointToStart(module.getBody()); std::string fname = llvm::sys::path::filename(filepath).str() + '\0'; + fname = (isZOS(module)) ? krnl::e2a_s(fname) : fname; mlir::StringAttr valueAttr = mlir::StringAttr::get(context, fname); create.llvm.globalOp(LLVM::LLVMArrayType::get(llvmI8Ty, fname.size()), /*isConstant=*/true, LLVM::Linkage::Internal, diff --git a/src/Runtime/OMExternalConstant.inc b/src/Runtime/OMExternalConstant.inc index 769924e788..44b234baa4 100644 --- a/src/Runtime/OMExternalConstant.inc +++ b/src/Runtime/OMExternalConstant.inc @@ -60,58 +60,39 @@ void checkEndianness(const char constPackIsLE) { /// This function is thread-safe. /// bool omMMapBinaryFile( - void **constAddr, char *filename, int64_t size, int64_t isLE) { - checkEndianness(isLE); - char *fname = filename; -#ifdef __MVS__ - // Convert the file name to EBCDIC for the open call. - char *tPath = strdup(fname); - if (!tPath) { - fprintf(stderr, "Error while strdup: %s", strerror(errno)); - return false; - } - __a2e_s(tPath); - fname = tPath; -#endif - + void **constAddr, char *fname, int64_t size, int64_t isLE) { if (constAddr == NULL) { fprintf(stderr, "Error: null pointer."); -#ifdef __MVS__ - free(tPath); -#endif return false; } - if (constAddr[0] == NULL) { - char *filePath; - char *basePath = getenv("OM_CONSTANT_PATH"); - if (basePath) { - size_t baseLen = strlen(basePath); - size_t fnameLen = strlen(fname); - size_t sepLen = strlen(DIR_SEPARATOR); - size_t filePathLen = baseLen + sepLen + fnameLen + 1; - filePath = (char *)malloc(filePathLen); - if (!filePath) { - fprintf(stderr, "Error while malloc: %s", strerror(errno)); -#ifdef __MVS__ - free(tPath); -#endif - return false; - } - snprintf(filePath, filePathLen, "%s%s%s", basePath, DIR_SEPARATOR, fname); - } else { - filePath = (char *)fname; - } - int fd = open(filePath, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Error while opening %s: %s\n", filePath, strerror(errno)); - if (basePath) - free(filePath); -#ifdef __MVS__ - free(tPath); -#endif + // Already mmaped. Nothing to do. + if (constAddr[0] != NULL) + return true; + + char *filePath; + char *basePath = getenv("OM_CONSTANT_PATH"); + if (basePath) { + size_t baseLen = strlen(basePath); + size_t fnameLen = strlen(fname); + size_t sepLen = strlen(DIR_SEPARATOR); + size_t filePathLen = baseLen + sepLen + fnameLen + 1; + filePath = (char *)malloc(filePathLen); + if (!filePath) { + fprintf(stderr, "Error while malloc: %s", strerror(errno)); return false; } + snprintf(filePath, filePathLen, "%s%s%s", basePath, DIR_SEPARATOR, fname); + } else { + filePath = (char *)fname; + } + int fd = open(filePath, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Error while opening %s: %s\n", filePath, strerror(errno)); + if (basePath) + free(filePath); + return false; + } #ifdef __MVS__ void *tempAddr = mmap(0, size, PROT_READ, __MAP_MEGA, fd, 0); @@ -119,45 +100,33 @@ bool omMMapBinaryFile( void *tempAddr = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); #endif - if (tempAddr == MAP_FAILED) { - fprintf(stderr, "Error while mmapping %s: %s\n", fname, strerror(errno)); - close(fd); - if (basePath) - free(filePath); -#ifdef __MVS__ - free(tPath); -#endif - return false; - } - - /* Prepare to compare-and-swap to setup the shared constAddr. - * If we fail, another thread beat us so free our mmap. - */ -#ifdef __MVS__ - void *expected = NULL; - if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) - munmap(tempAddr, size); -#else - if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) - munmap(tempAddr, size); -#endif - - /* Either we succeeded in setting constAddr or someone else did it. - * Either way, constAddr is now setup. We can close our fd without - * invalidating the mmap. - */ + if (tempAddr == MAP_FAILED) { + fprintf(stderr, "Error while mmapping %s: %s\n", fname, strerror(errno)); close(fd); if (basePath) free(filePath); -#ifdef __MVS__ - free(tPath); -#endif - return true; + return false; } + /* Prepare to compare-and-swap to setup the shared constAddr. + * If we fail, another thread beat us so free our mmap. + */ #ifdef __MVS__ - free(tPath); + void *expected = NULL; + if (cds((cds_t *)&expected, (cds_t *)&constAddr[0], *(cds_t *)&tempAddr)) + munmap(tempAddr, size); +#else + if (!__sync_bool_compare_and_swap(&constAddr[0], NULL, tempAddr)) + munmap(tempAddr, size); #endif + + /* Either we succeeded in setting constAddr or someone else did it. + * Either way, constAddr is now setup. We can close our fd without + * invalidating the mmap. + */ + close(fd); + if (basePath) + free(filePath); return true; }