From b52112275d464ce9417d94ecb610e3fc87101a10 Mon Sep 17 00:00:00 2001 From: Stephan Herhut Date: Thu, 23 May 2019 02:03:14 -0700 Subject: [PATCH] Add LLVM::IntToPtrOp and LLVM::PtrToIntOp to LLVM dialect. -- PiperOrigin-RevId: 249604199 --- mlir/include/mlir/LLVMIR/LLVMOps.td | 21 +++++++++++--- mlir/lib/IR/Operation.cpp | 7 +++-- mlir/lib/LLVMIR/IR/LLVMDialect.cpp | 45 ----------------------------- mlir/test/LLVMIR/roundtrip.mlir | 10 ++++++- mlir/test/Target/llvmir.mlir | 9 ++++++ 5 files changed, 40 insertions(+), 52 deletions(-) diff --git a/mlir/include/mlir/LLVMIR/LLVMOps.td b/mlir/include/mlir/LLVMIR/LLVMOps.td index 43b2bc54c56a..fed49d9c5773 100644 --- a/mlir/include/mlir/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/LLVMIR/LLVMOps.td @@ -118,7 +118,7 @@ class LLVM_ArithmeticOp, LLVM_Builder<"$res = builder." # builderFunc # "($lhs, $rhs);"> { let parser = [{ return impl::parseBinaryOp(parser, result); }]; - let printer = [{ printLLVMBinaryOp(p, this->getOperation()); }]; + let printer = [{ mlir::impl::printBinaryOp(this->getOperation(), p); }]; } // Integer binary operations. @@ -183,10 +183,23 @@ def LLVM_BitcastOp : LLVM_OneResultOp<"bitcast", [NoSideEffect]>, Arguments<(ins LLVM_Type:$arg)>, LLVM_Builder<"$res = builder.CreateBitCast($arg, $_resultType);"> { - let parser = [{ return parseBitcastOp(parser, result); }]; - let printer = [{ printBitcastOp(p, *this); }]; + let parser = [{ return mlir::impl::parseCastOp(parser, result); }]; + let printer = [{ mlir::impl::printCastOp(this->getOperation(), p); }]; +} +def LLVM_IntToPtrOp + : LLVM_OneResultOp<"inttoptr", [NoSideEffect]>, + Arguments<(ins LLVM_Type:$arg)>, + LLVM_Builder<"$res = builder.CreateIntToPtr($arg, $_resultType);"> { + let parser = [{ return mlir::impl::parseCastOp(parser, result); }]; + let printer = [{ mlir::impl::printCastOp(this->getOperation(), p); }]; +} +def LLVM_PtrToIntOp + : LLVM_OneResultOp<"ptrtoint", [NoSideEffect]>, + Arguments<(ins LLVM_Type:$arg)>, + LLVM_Builder<"$res = builder.CreatePtrToInt($arg, $_resultType);"> { + let parser = [{ return mlir::impl::parseCastOp(parser, result); }]; + let printer = [{ mlir::impl::printCastOp(this->getOperation(), p); }]; } - // Call-related operations. def LLVM_CallOp : LLVM_Op<"call">, Arguments<(ins OptionalAttr:$callee, diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp index 0d93acb13604..22463f1bd768 100644 --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -990,6 +990,7 @@ ParseResult impl::parseCastOp(OpAsmParser *parser, OperationState *result) { OpAsmParser::OperandType srcInfo; Type srcType, dstType; return failure(parser->parseOperand(srcInfo) || + parser->parseOptionalAttributeDict(result->attributes) || parser->parseColonType(srcType) || parser->resolveOperand(srcInfo, srcType, result->operands) || parser->parseKeywordType("to", dstType) || @@ -997,8 +998,10 @@ ParseResult impl::parseCastOp(OpAsmParser *parser, OperationState *result) { } void impl::printCastOp(Operation *op, OpAsmPrinter *p) { - *p << op->getName() << ' ' << *op->getOperand(0) << " : " - << op->getOperand(0)->getType() << " to " << op->getResult(0)->getType(); + *p << op->getName() << ' ' << *op->getOperand(0); + p->printOptionalAttrDict(op->getAttrs()); + *p << " : " << op->getOperand(0)->getType() << " to " + << op->getResult(0)->getType(); } Value *impl::foldCastOp(Operation *op) { diff --git a/mlir/lib/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/LLVMIR/IR/LLVMDialect.cpp index 950b1d41e279..8b673f37664c 100644 --- a/mlir/lib/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/LLVMIR/IR/LLVMDialect.cpp @@ -35,24 +35,6 @@ using namespace mlir; using namespace mlir::LLVM; -static void printLLVMBinaryOp(OpAsmPrinter *p, Operation *op) { - // Fallback to the generic form if the op is not well-formed (may happen - // during incomplete rewrites, and used for debugging). - const auto *abstract = op->getAbstractOperation(); - (void)abstract; - assert(abstract && "pretty printing an unregistered operation"); - - auto resultType = op->getResult(0)->getType(); - if (resultType != op->getOperand(0)->getType() || - resultType != op->getOperand(1)->getType()) - return p->printGenericOp(op); - - *p << op->getName().getStringRef() << ' ' << *op->getOperand(0) << ", " - << *op->getOperand(1); - p->printOptionalAttrDict(op->getAttrs()); - *p << " : " << op->getResult(0)->getType(); -} - //===----------------------------------------------------------------------===// // Printing/parsing for LLVM::ICmpOp. //===----------------------------------------------------------------------===// @@ -321,33 +303,6 @@ static ParseResult parseStoreOp(OpAsmParser *parser, OperationState *result) { return success(); } -//===----------------------------------------------------------------------===// -// Printing/parsing for LLVM::BitcastOp. -//===----------------------------------------------------------------------===// - -static void printBitcastOp(OpAsmPrinter *p, BitcastOp &op) { - *p << op.getOperationName() << ' ' << *op.arg(); - p->printOptionalAttrDict(op.getAttrs()); - *p << " : " << op.arg()->getType() << " to " << op.getType(); -} - -// ::= `llvm.bitcast` ssa-use attribute-dict? `:` type `to` type -static ParseResult parseBitcastOp(OpAsmParser *parser, OperationState *result) { - SmallVector attrs; - OpAsmParser::OperandType arg; - Type sourceType, type; - - if (parser->parseOperand(arg) || parser->parseOptionalAttributeDict(attrs) || - parser->parseColonType(sourceType) || parser->parseKeyword("to") || - parser->parseType(type) || - parser->resolveOperand(arg, sourceType, result->operands)) - return failure(); - - result->attributes = attrs; - result->addTypes(type); - return success(); -} - //===----------------------------------------------------------------------===// // Printing/parsing for LLVM::CallOp. //===----------------------------------------------------------------------===// diff --git a/mlir/test/LLVMIR/roundtrip.mlir b/mlir/test/LLVMIR/roundtrip.mlir index f4b0c30cef9a..39805f9ae2a9 100644 --- a/mlir/test/LLVMIR/roundtrip.mlir +++ b/mlir/test/LLVMIR/roundtrip.mlir @@ -78,8 +78,16 @@ func @ops(%arg0 : !llvm.i32, %arg1 : !llvm.float) { // Misc operations. // CHECK: %24 = llvm.select %7, %0, %1 : !llvm.i1, !llvm.i32 -// CHECK-NEXT: llvm.return %24 = llvm.select %7, %0, %1 : !llvm.i1, !llvm.i32 + +// Integer to pointer and pointer to integer conversions. +// +// CHECK: %25 = llvm.inttoptr %arg0 : !llvm.i32 to !llvm<"i32*"> +// CHECK-NEXT: %26 = llvm.ptrtoint %25 : !llvm<"i32*"> to !llvm.i32 + %25 = llvm.inttoptr %arg0 : !llvm.i32 to !llvm<"i32*"> + %26 = llvm.ptrtoint %25 : !llvm<"i32*"> to !llvm.i32 + +// CHECK: llvm.return llvm.return } diff --git a/mlir/test/Target/llvmir.mlir b/mlir/test/Target/llvmir.mlir index ad0a2425b811..fca047596e5d 100644 --- a/mlir/test/Target/llvmir.mlir +++ b/mlir/test/Target/llvmir.mlir @@ -810,3 +810,12 @@ func @llvm_noalias(%arg0: !llvm<"float*"> {llvm.noalias: true}) { // CHECK-LABEL: @llvm_varargs(...) func @llvm_varargs() attributes {std.varargs: true} + +func @intpointerconversion(%arg0 : !llvm.i32) -> !llvm.i32 { +// CHECK: %2 = inttoptr i32 %0 to i32* +// CHECK-NEXT: %3 = ptrtoint i32* %2 to i32 + %1 = llvm.inttoptr %arg0 : !llvm.i32 to !llvm<"i32*"> + %2 = llvm.ptrtoint %1 : !llvm<"i32*"> to !llvm.i32 + llvm.return %2 : !llvm.i32 +} +