[RefE2E] Fix nul-termination bug.

I was seeing some of the error messages come out with some garbage at
the end. This fixes it.
pull/51/head
Sean Silva 2020-09-17 19:03:33 -07:00
parent 2284f6b4f1
commit 62738d3641
2 changed files with 10 additions and 7 deletions

View File

@ -153,10 +153,13 @@ public:
static LLVM::GlobalOp createGlobalString(ModuleOp module, StringAttr msg, static LLVM::GlobalOp createGlobalString(ModuleOp module, StringAttr msg,
OpBuilder &builder, Location loc) { OpBuilder &builder, Location loc) {
// TODO: Deduplicate strings. // TODO: Deduplicate strings.
std::string msgNulTerminated = msg.getValue().str();
msgNulTerminated.push_back('\0');
auto arrayTy = LLVMType::getArrayTy(LLVMType::getInt8Ty(module.getContext()), auto arrayTy = LLVMType::getArrayTy(LLVMType::getInt8Ty(module.getContext()),
msg.getValue().size()); msgNulTerminated.size());
OpBuilder::InsertionGuard guard(builder); OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(module.getBody()); builder.setInsertionPointToStart(module.getBody());
// To get a unique symbol name, use a suffix derived from the current number // To get a unique symbol name, use a suffix derived from the current number
// of ops in the module. // of ops in the module.
// We can't use the SymbolTable's logic for this because the module // We can't use the SymbolTable's logic for this because the module
@ -166,9 +169,9 @@ static LLVM::GlobalOp createGlobalString(ModuleOp module, StringAttr msg,
(Twine("__npcomp_string_") + (Twine("__npcomp_string_") +
Twine(llvm::size(llvm::to_vector<6>(module.getOps<LLVM::GlobalOp>())))) Twine(llvm::size(llvm::to_vector<6>(module.getOps<LLVM::GlobalOp>()))))
.str(); .str();
auto globalOp = auto globalOp = builder.create<LLVM::GlobalOp>(
builder.create<LLVM::GlobalOp>(loc, arrayTy, /*isConstant=*/true, loc, arrayTy, /*isConstant=*/true, LLVM::Linkage::Internal, symbolName,
LLVM::Linkage::Internal, symbolName, msg); builder.getStringAttr(msgNulTerminated));
return globalOp; return globalOp;
} }

View File

@ -211,7 +211,7 @@ func @inputs1results2(%arg0: !npcomprt.tensor) -> (!npcomprt.tensor, !npcomprt.t
// Test emission of compiler runtime functions. // Test emission of compiler runtime functions.
// CHECK: llvm.mlir.global internal constant @[[STRSYM:.*]]("msg") // CHECK: llvm.mlir.global internal constant @[[STRSYM:.*]]("msg\00")
// CHECK: llvm.func @__npcomp_compiler_rt_abort_if(!llvm.i1, !llvm.ptr<i8>) // CHECK: llvm.func @__npcomp_compiler_rt_abort_if(!llvm.i1, !llvm.ptr<i8>)
// CHECK: llvm.func @__npcomp_compiler_rt_to_memref(!llvm.ptr<i8>) -> !llvm.struct<(i64, ptr<i8>)> // CHECK: llvm.func @__npcomp_compiler_rt_to_memref(!llvm.ptr<i8>) -> !llvm.struct<(i64, ptr<i8>)>
// CHECK: llvm.func @__npcomp_compiler_rt_from_memref(!llvm.i64, !llvm.ptr<i8>) -> !llvm.ptr<i8> // CHECK: llvm.func @__npcomp_compiler_rt_from_memref(!llvm.i64, !llvm.ptr<i8>) -> !llvm.ptr<i8>
@ -219,9 +219,9 @@ func @inputs1results2(%arg0: !npcomprt.tensor) -> (!npcomprt.tensor, !npcomprt.t
// CHECK-LABEL: llvm.func @calls_abort_if( // CHECK-LABEL: llvm.func @calls_abort_if(
// CHECK-SAME: %[[VAL_0:.*]]: !llvm.i1) { // CHECK-SAME: %[[VAL_0:.*]]: !llvm.i1) {
// CHECK: %[[VAL_0:.*]] = llvm.mlir.addressof @[[STRSYM]] : !llvm.ptr<array<3 x i8>> // CHECK: %[[VAL_0:.*]] = llvm.mlir.addressof @[[STRSYM]] : !llvm.ptr<array<4 x i8>>
// CHECK: %[[VAL_1:.*]] = llvm.mlir.constant(0 : i32) : !llvm.i32 // CHECK: %[[VAL_1:.*]] = llvm.mlir.constant(0 : i32) : !llvm.i32
// CHECK: %[[VAL_2:.*]] = llvm.getelementptr %[[VAL_0]]{{\[}}%[[VAL_1]], %[[VAL_1]]] : (!llvm.ptr<array<3 x i8>>, !llvm.i32, !llvm.i32) -> !llvm.ptr<i8> // CHECK: %[[VAL_2:.*]] = llvm.getelementptr %[[VAL_0]]{{\[}}%[[VAL_1]], %[[VAL_1]]] : (!llvm.ptr<array<4 x i8>>, !llvm.i32, !llvm.i32) -> !llvm.ptr<i8>
// CHECK: llvm.call @__npcomp_compiler_rt_abort_if(%[[VAL_3:.*]], %[[VAL_2]]) : (!llvm.i1, !llvm.ptr<i8>) -> () // CHECK: llvm.call @__npcomp_compiler_rt_abort_if(%[[VAL_3:.*]], %[[VAL_2]]) : (!llvm.i1, !llvm.ptr<i8>) -> ()
// CHECK: llvm.return // CHECK: llvm.return