[mlir][ODS] Use c++ types for integer attributes of fixed width when possible.

Unsigned and Signless attributes use uintN_t and signed attributes use intN_t, where N is the fixed width. The 1-bit variants use bool.

Differential Revision: https://reviews.llvm.org/D86739
This commit is contained in:
River Riddle 2020-09-01 13:32:14 -07:00
parent 137dfd616a
commit 431bb8b318
23 changed files with 109 additions and 124 deletions

View File

@ -365,7 +365,7 @@ void StructAccessOp::build(mlir::OpBuilder &b, mlir::OperationState &state,
static mlir::LogicalResult verify(StructAccessOp op) { static mlir::LogicalResult verify(StructAccessOp op) {
StructType structTy = op.input().getType().cast<StructType>(); StructType structTy = op.input().getType().cast<StructType>();
size_t index = op.index().getZExtValue(); size_t index = op.index();
if (index >= structTy.getNumElementTypes()) if (index >= structTy.getNumElementTypes())
return op.emitOpError() return op.emitOpError()
<< "index should be within the range of the input struct type"; << "index should be within the range of the input struct type";

View File

@ -42,7 +42,7 @@ OpFoldResult StructAccessOp::fold(ArrayRef<Attribute> operands) {
if (!structAttr) if (!structAttr)
return nullptr; return nullptr;
size_t elementIndex = index().getZExtValue(); size_t elementIndex = index();
return structAttr[elementIndex]; return structAttr[elementIndex];
} }

View File

@ -215,17 +215,9 @@ def LLVM_FNegOp : LLVM_UnaryArithmeticOp<"fneg", "CreateFNeg">;
// Common code definition that is used to verify and set the alignment attribute // Common code definition that is used to verify and set the alignment attribute
// of LLVM ops that accept such an attribute. // of LLVM ops that accept such an attribute.
class MemoryOpWithAlignmentBase { class MemoryOpWithAlignmentBase {
code alignmentVerifierCode = [{
if (alignment().hasValue()) {
auto align = alignment().getValue().getSExtValue();
if (align < 0)
return emitOpError("expected positive alignment");
}
return success();
}];
code setAlignmentCode = [{ code setAlignmentCode = [{
if ($alignment.hasValue()) { if ($alignment.hasValue()) {
auto align = $alignment.getValue().getZExtValue(); auto align = $alignment.getValue();
if (align != 0) if (align != 0)
inst->setAlignment(llvm::Align(align)); inst->setAlignment(llvm::Align(align));
} }
@ -266,7 +258,6 @@ def LLVM_AllocaOp :
}]>]; }]>];
let parser = [{ return parseAllocaOp(parser, result); }]; let parser = [{ return parseAllocaOp(parser, result); }];
let printer = [{ printAllocaOp(p, *this); }]; let printer = [{ printAllocaOp(p, *this); }];
let verifier = alignmentVerifierCode;
} }
def LLVM_GEPOp : LLVM_OneResultOp<"getelementptr", [NoSideEffect]>, def LLVM_GEPOp : LLVM_OneResultOp<"getelementptr", [NoSideEffect]>,
Arguments<(ins LLVM_Type:$base, Variadic<LLVM_Type>:$indices)>, Arguments<(ins LLVM_Type:$base, Variadic<LLVM_Type>:$indices)>,
@ -301,7 +292,6 @@ def LLVM_LoadOp :
"bool isNonTemporal = false">]; "bool isNonTemporal = false">];
let parser = [{ return parseLoadOp(parser, result); }]; let parser = [{ return parseLoadOp(parser, result); }];
let printer = [{ printLoadOp(p, *this); }]; let printer = [{ printLoadOp(p, *this); }];
let verifier = alignmentVerifierCode;
} }
def LLVM_StoreOp : def LLVM_StoreOp :
MemoryOpWithAlignmentAndAttributes, MemoryOpWithAlignmentAndAttributes,
@ -321,7 +311,6 @@ def LLVM_StoreOp :
]; ];
let parser = [{ return parseStoreOp(parser, result); }]; let parser = [{ return parseStoreOp(parser, result); }];
let printer = [{ printStoreOp(p, *this); }]; let printer = [{ printStoreOp(p, *this); }];
let verifier = alignmentVerifierCode;
} }
// Casts. // Casts.
@ -646,7 +635,7 @@ def LLVM_AddressOfOp
OpBuilder<"OpBuilder &builder, OperationState &result, GlobalOp global, " OpBuilder<"OpBuilder &builder, OperationState &result, GlobalOp global, "
"ArrayRef<NamedAttribute> attrs = {}", [{ "ArrayRef<NamedAttribute> attrs = {}", [{
build(builder, result, build(builder, result,
global.getType().getPointerTo(global.addr_space().getZExtValue()), global.getType().getPointerTo(global.addr_space()),
global.sym_name(), attrs);}]>, global.sym_name(), attrs);}]>,
OpBuilder<"OpBuilder &builder, OperationState &result, LLVMFuncOp func, " OpBuilder<"OpBuilder &builder, OperationState &result, LLVMFuncOp func, "
@ -917,8 +906,8 @@ def LLVM_MatrixColumnMajorLoadOp
llvm::Align align = dl.getABITypeAlign( llvm::Align align = dl.getABITypeAlign(
$data->getType()->getPointerElementType()); $data->getType()->getPointerElementType());
$res = mb.CreateColumnMajorLoad( $res = mb.CreateColumnMajorLoad(
$data, align, $stride, $isVolatile.getZExtValue(), $rows.getZExtValue(), $data, align, $stride, $isVolatile, $rows,
$columns.getZExtValue()); $columns);
}]; }];
let assemblyFormat = "$data `,` `<` `stride` `=` $stride `>` attr-dict" let assemblyFormat = "$data `,` `<` `stride` `=` $stride `>` attr-dict"
"`:` type($res) `from` type($data) `stride` type($stride)"; "`:` type($res) `from` type($data) `stride` type($stride)";
@ -943,8 +932,8 @@ def LLVM_MatrixColumnMajorStoreOp
llvm::Align align = dl.getABITypeAlign( llvm::Align align = dl.getABITypeAlign(
$data->getType()->getPointerElementType()); $data->getType()->getPointerElementType());
mb.CreateColumnMajorStore( mb.CreateColumnMajorStore(
$matrix, $data, align, $stride, $isVolatile.getZExtValue(), $matrix, $data, align, $stride, $isVolatile,
$rows.getZExtValue(), $columns.getZExtValue()); $rows, $columns);
}]; }];
let assemblyFormat = "$matrix `,` $data `,` `<` `stride` `=` $stride `>` " let assemblyFormat = "$matrix `,` $data `,` `<` `stride` `=` $stride `>` "
"attr-dict`:` type($matrix) `to` type($data) `stride` type($stride)"; "attr-dict`:` type($matrix) `to` type($data) `stride` type($stride)";
@ -960,8 +949,8 @@ def LLVM_MatrixMultiplyOp
string llvmBuilder = [{ string llvmBuilder = [{
llvm::MatrixBuilder<decltype(builder)> mb(builder); llvm::MatrixBuilder<decltype(builder)> mb(builder);
$res = mb.CreateMatrixMultiply( $res = mb.CreateMatrixMultiply(
$lhs, $rhs, $lhs_rows.getZExtValue(), $lhs_columns.getZExtValue(), $lhs, $rhs, $lhs_rows, $lhs_columns,
$rhs_columns.getZExtValue()); $rhs_columns);
}]; }];
let assemblyFormat = "$lhs `,` $rhs attr-dict " let assemblyFormat = "$lhs `,` $rhs attr-dict "
"`:` `(` type($lhs) `,` type($rhs) `)` `->` type($res)"; "`:` `(` type($lhs) `,` type($rhs) `)` `->` type($res)";
@ -975,7 +964,7 @@ def LLVM_MatrixTransposeOp
string llvmBuilder = [{ string llvmBuilder = [{
llvm::MatrixBuilder<decltype(builder)> mb(builder); llvm::MatrixBuilder<decltype(builder)> mb(builder);
$res = mb.CreateMatrixTranspose( $res = mb.CreateMatrixTranspose(
$matrix, $rows.getZExtValue(), $columns.getZExtValue()); $matrix, $rows, $columns);
}]; }];
let assemblyFormat = "$matrix attr-dict `:` type($matrix) `into` type($res)"; let assemblyFormat = "$matrix attr-dict `:` type($matrix) `into` type($res)";
} }
@ -999,9 +988,9 @@ def LLVM_MaskedLoadOp
Variadic<LLVM_Type>:$pass_thru, I32Attr:$alignment)> { Variadic<LLVM_Type>:$pass_thru, I32Attr:$alignment)> {
string llvmBuilder = [{ string llvmBuilder = [{
$res = $pass_thru.empty() ? builder.CreateMaskedLoad( $res = $pass_thru.empty() ? builder.CreateMaskedLoad(
$data, llvm::Align($alignment.getZExtValue()), $mask) : $data, llvm::Align($alignment), $mask) :
builder.CreateMaskedLoad( builder.CreateMaskedLoad(
$data, llvm::Align($alignment.getZExtValue()), $mask, $pass_thru[0]); $data, llvm::Align($alignment), $mask, $pass_thru[0]);
}]; }];
let assemblyFormat = let assemblyFormat =
"operands attr-dict `:` functional-type(operands, results)"; "operands attr-dict `:` functional-type(operands, results)";
@ -1014,7 +1003,7 @@ def LLVM_MaskedStoreOp
I32Attr:$alignment)> { I32Attr:$alignment)> {
string llvmBuilder = [{ string llvmBuilder = [{
builder.CreateMaskedStore( builder.CreateMaskedStore(
$value, $data, llvm::Align($alignment.getZExtValue()), $mask); $value, $data, llvm::Align($alignment), $mask);
}]; }];
let assemblyFormat = "$value `,` $data `,` $mask attr-dict `:` " let assemblyFormat = "$value `,` $data `,` $mask attr-dict `:` "
"type($value) `,` type($mask) `into` type($data)"; "type($value) `,` type($mask) `into` type($data)";
@ -1027,9 +1016,9 @@ def LLVM_masked_gather
Variadic<LLVM_Type>:$pass_thru, I32Attr:$alignment)> { Variadic<LLVM_Type>:$pass_thru, I32Attr:$alignment)> {
string llvmBuilder = [{ string llvmBuilder = [{
$res = $pass_thru.empty() ? builder.CreateMaskedGather( $res = $pass_thru.empty() ? builder.CreateMaskedGather(
$ptrs, llvm::Align($alignment.getZExtValue()), $mask) : $ptrs, llvm::Align($alignment), $mask) :
builder.CreateMaskedGather( builder.CreateMaskedGather(
$ptrs, llvm::Align($alignment.getZExtValue()), $mask, $pass_thru[0]); $ptrs, llvm::Align($alignment), $mask, $pass_thru[0]);
}]; }];
let assemblyFormat = let assemblyFormat =
"operands attr-dict `:` functional-type(operands, results)"; "operands attr-dict `:` functional-type(operands, results)";
@ -1042,7 +1031,7 @@ def LLVM_masked_scatter
I32Attr:$alignment)> { I32Attr:$alignment)> {
string llvmBuilder = [{ string llvmBuilder = [{
builder.CreateMaskedScatter( builder.CreateMaskedScatter(
$value, $ptrs, llvm::Align($alignment.getZExtValue()), $mask); $value, $ptrs, llvm::Align($alignment), $mask);
}]; }];
let assemblyFormat = "$value `,` $ptrs `,` $mask attr-dict `:` " let assemblyFormat = "$value `,` $ptrs `,` $mask attr-dict `:` "
"type($value) `,` type($mask) `into` type($ptrs)"; "type($value) `,` type($mask) `into` type($ptrs)";

View File

@ -477,9 +477,9 @@ class GenericOpBase<string mnemonic> : LinalgStructuredBase_Op<mnemonic,
}; };
} }
unsigned getNumInputs() { return args_in().getSExtValue(); } unsigned getNumInputs() { return args_in(); }
unsigned getNumOutputs() { return args_out().getSExtValue(); } unsigned getNumOutputs() { return args_out(); }
StringRef getLibraryCallName() { StringRef getLibraryCallName() {
return library_call().hasValue() ? library_call().getValue() : ""; return library_call().hasValue() ? library_call().getValue() : "";
@ -498,7 +498,7 @@ class GenericOpBase<string mnemonic> : LinalgStructuredBase_Op<mnemonic,
llvm::Optional<unsigned> getSymbolSource() { llvm::Optional<unsigned> getSymbolSource() {
auto ss = symbol_source(); auto ss = symbol_source();
return ss.hasValue() ? return ss.hasValue() ?
llvm::Optional<unsigned>(ss.getValue().getLimitedValue()) : llvm::None; llvm::Optional<unsigned>(ss.getValue()) : llvm::None;
} }
}]; }];

View File

@ -248,7 +248,7 @@ def quant_StatisticsOp : quant_Op<"stats", [SameOperandsAndResultType]> {
auto shape = tensorArg.getShape(); auto shape = tensorArg.getShape();
auto argSliceSize = std::accumulate(std::next(shape.begin(), auto argSliceSize = std::accumulate(std::next(shape.begin(),
axis()->getSExtValue()), shape.end(), 1, std::multiplies<int64_t>()); *axis()), shape.end(), 1, std::multiplies<int64_t>());
auto axisStatsType = axisStats()->getType(); auto axisStatsType = axisStats()->getType();
if (!axisStatsType.getElementType().isa<FloatType>()) { if (!axisStatsType.getElementType().isa<FloatType>()) {

View File

@ -2045,20 +2045,6 @@ def PrefetchOp : Std_Op<"prefetch"> {
IntMaxValue<3>]>:$localityHint, IntMaxValue<3>]>:$localityHint,
BoolAttr:$isDataCache); BoolAttr:$isDataCache);
let builders = [OpBuilder<
"OpBuilder &builder, OperationState &result, Value memref,"
"ArrayRef<Value> indices, bool isWrite, unsigned hint, bool isData",
[{
auto hintAttr = builder.getI32IntegerAttr(hint);
auto isWriteAttr = builder.getBoolAttr(isWrite);
auto isDataCacheAttr = builder.getBoolAttr(isData);
result.addOperands(memref);
result.addOperands(indices);
result.addAttribute("localityHint", hintAttr);
result.addAttribute("isWrite", isWriteAttr);
result.addAttribute("isDataCache", isDataCacheAttr);
}]>];
let extraClassDeclaration = [{ let extraClassDeclaration = [{
MemRefType getMemRefType() { MemRefType getMemRefType() {
return memref().getType().cast<MemRefType>(); return memref().getType().cast<MemRefType>();

View File

@ -904,12 +904,24 @@ class SignlessIntegerAttrBase<I attrValType, string descr> :
descr> { descr> {
let returnType = [{ ::llvm::APInt }]; let returnType = [{ ::llvm::APInt }];
} }
// Base class for signless integer attributes of fixed width that have a
// correpsonding C++ type.
class TypedSignlessIntegerAttrBase<I attrValType, string retType, string descr>
: SignlessIntegerAttrBase<attrValType, descr> {
let returnType = retType;
let convertFromStorage = "$_self.getValue().getZExtValue()";
}
def I1Attr : SignlessIntegerAttrBase<I1, "1-bit signless integer attribute">; def I1Attr : TypedSignlessIntegerAttrBase<
def I8Attr : SignlessIntegerAttrBase<I8, "8-bit signless integer attribute">; I1, "bool", "1-bit signless integer attribute">;
def I16Attr : SignlessIntegerAttrBase<I16, "16-bit signless integer attribute">; def I8Attr : TypedSignlessIntegerAttrBase<
def I32Attr : SignlessIntegerAttrBase<I32, "32-bit signless integer attribute">; I8, "uint8_t", "8-bit signless integer attribute">;
def I64Attr : SignlessIntegerAttrBase<I64, "64-bit signless integer attribute">; def I16Attr : TypedSignlessIntegerAttrBase<
I16, "uint16_t", "16-bit signless integer attribute">;
def I32Attr : TypedSignlessIntegerAttrBase<
I32, "uint32_t", "32-bit signless integer attribute">;
def I64Attr : TypedSignlessIntegerAttrBase<
I64, "uint64_t", "64-bit signless integer attribute">;
// Base class for signed integer attributes of fixed width. // Base class for signed integer attributes of fixed width.
class SignedIntegerAttrBase<SI attrValType, string descr> : class SignedIntegerAttrBase<SI attrValType, string descr> :
@ -921,17 +933,24 @@ class SignedIntegerAttrBase<SI attrValType, string descr> :
descr> { descr> {
let returnType = [{ ::llvm::APInt }]; let returnType = [{ ::llvm::APInt }];
} }
// Base class for signed integer attributes of fixed width that have a
// correpsonding C++ type.
class TypedSignedIntegerAttrBase<SI attrValType, string retType, string descr>
: SignedIntegerAttrBase<attrValType, descr> {
let returnType = retType;
let convertFromStorage = "$_self.getValue().getSExtValue()";
}
def SI1Attr : SignedIntegerAttrBase< def SI1Attr : TypedSignedIntegerAttrBase<
SI1, "1-bit signed integer attribute">; SI1, "bool", "1-bit signed integer attribute">;
def SI8Attr : SignedIntegerAttrBase< def SI8Attr : TypedSignedIntegerAttrBase<
SI8, "8-bit signed integer attribute">; SI8, "int8_t", "8-bit signed integer attribute">;
def SI16Attr : SignedIntegerAttrBase< def SI16Attr : TypedSignedIntegerAttrBase<
SI16, "16-bit signed integer attribute">; SI16, "int16_t", "16-bit signed integer attribute">;
def SI32Attr : SignedIntegerAttrBase< def SI32Attr : TypedSignedIntegerAttrBase<
SI32, "32-bit signed integer attribute">; SI32, "int32_t", "32-bit signed integer attribute">;
def SI64Attr : SignedIntegerAttrBase< def SI64Attr : TypedSignedIntegerAttrBase<
SI64, "64-bit signed integer attribute">; SI64, "int64_t", "64-bit signed integer attribute">;
// Base class for unsigned integer attributes of fixed width. // Base class for unsigned integer attributes of fixed width.
class UnsignedIntegerAttrBase<UI attrValType, string descr> : class UnsignedIntegerAttrBase<UI attrValType, string descr> :
@ -943,17 +962,24 @@ class UnsignedIntegerAttrBase<UI attrValType, string descr> :
descr> { descr> {
let returnType = [{ ::llvm::APInt }]; let returnType = [{ ::llvm::APInt }];
} }
// Base class for unsigned integer attributes of fixed width that have a
// correpsonding C++ type.
class TypedUnsignedIntegerAttrBase<UI attrValType, string retType, string descr>
: UnsignedIntegerAttrBase<attrValType, descr> {
let returnType = retType;
let convertFromStorage = "$_self.getValue().getZExtValue()";
}
def UI1Attr : UnsignedIntegerAttrBase< def UI1Attr : TypedUnsignedIntegerAttrBase<
UI1, "1-bit unsigned integer attribute">; UI1, "bool", "1-bit unsigned integer attribute">;
def UI8Attr : UnsignedIntegerAttrBase< def UI8Attr : TypedUnsignedIntegerAttrBase<
UI8, "8-bit unsigned integer attribute">; UI8, "uint8_t", "8-bit unsigned integer attribute">;
def UI16Attr : UnsignedIntegerAttrBase< def UI16Attr : TypedUnsignedIntegerAttrBase<
UI16, "16-bit unsigned integer attribute">; UI16, "uint16_t", "16-bit unsigned integer attribute">;
def UI32Attr : UnsignedIntegerAttrBase< def UI32Attr : TypedUnsignedIntegerAttrBase<
UI32, "32-bit unsigned integer attribute">; UI32, "uint32_t", "32-bit unsigned integer attribute">;
def UI64Attr : UnsignedIntegerAttrBase< def UI64Attr : TypedUnsignedIntegerAttrBase<
UI64, "64-bit unsigned integer attribute">; UI64, "uint64_t", "64-bit unsigned integer attribute">;
// Base class for float attributes of fixed width. // Base class for float attributes of fixed width.
class FloatAttrBase<F attrValType, string descr> : class FloatAttrBase<F attrValType, string descr> :

View File

@ -502,9 +502,9 @@ public:
return failure(); return failure();
// Build std.prefetch memref[expandedMap.results]. // Build std.prefetch memref[expandedMap.results].
rewriter.replaceOpWithNewOp<PrefetchOp>( rewriter.replaceOpWithNewOp<PrefetchOp>(op, op.memref(), *resultOperands,
op, op.memref(), *resultOperands, op.isWrite(), op.isWrite(), op.localityHint(),
op.localityHint().getZExtValue(), op.isDataCache()); op.isDataCache());
return success(); return success();
} }
}; };

View File

@ -100,8 +100,8 @@ struct GPUFuncOpLowering : ConvertToLLVMPattern {
Value address = rewriter.create<LLVM::AddressOfOp>(loc, global); Value address = rewriter.create<LLVM::AddressOfOp>(loc, global);
auto elementType = global.getType().getArrayElementType(); auto elementType = global.getType().getArrayElementType();
Value memory = rewriter.create<LLVM::GEPOp>( Value memory = rewriter.create<LLVM::GEPOp>(
loc, elementType.getPointerTo(global.addr_space().getZExtValue()), loc, elementType.getPointerTo(global.addr_space()), address,
address, ArrayRef<Value>{zero, zero}); ArrayRef<Value>{zero, zero});
// Build a memref descriptor pointing to the buffer to plug with the // Build a memref descriptor pointing to the buffer to plug with the
// existing memref infrastructure. This may use more registers than // existing memref infrastructure. This may use more registers than

View File

@ -71,8 +71,7 @@ SingleWorkgroupReduction::matchAsPerformingReduction(
return llvm::None; return llvm::None;
// Make sure this is reduction with one input and one output. // Make sure this is reduction with one input and one output.
if (genericOp.args_in().getZExtValue() != 1 || if (genericOp.args_in() != 1 || genericOp.args_out() != 1)
genericOp.args_out().getZExtValue() != 1)
return llvm::None; return llvm::None;
auto originalInputType = op->getOperand(0).getType().cast<MemRefType>(); auto originalInputType = op->getOperand(0).getType().cast<MemRefType>();

View File

@ -766,9 +766,8 @@ public:
case spirv::MemoryAccess::None: case spirv::MemoryAccess::None:
case spirv::MemoryAccess::Nontemporal: case spirv::MemoryAccess::Nontemporal:
case spirv::MemoryAccess::Volatile: { case spirv::MemoryAccess::Volatile: {
unsigned alignment = memoryAccess == spirv::MemoryAccess::Aligned unsigned alignment =
? op.alignment().getValue().getZExtValue() memoryAccess == spirv::MemoryAccess::Aligned ? *op.alignment() : 0;
: 0;
bool isNonTemporal = memoryAccess == spirv::MemoryAccess::Nontemporal; bool isNonTemporal = memoryAccess == spirv::MemoryAccess::Nontemporal;
bool isVolatile = memoryAccess == spirv::MemoryAccess::Volatile; bool isVolatile = memoryAccess == spirv::MemoryAccess::Volatile;
replaceWithLoadOrStore(op, rewriter, this->typeConverter, alignment, replaceWithLoadOrStore(op, rewriter, this->typeConverter, alignment,

View File

@ -70,7 +70,6 @@ public:
LogicalResult LogicalResult
matchAndRewrite(ConstSizeOp op, ArrayRef<Value> operands, matchAndRewrite(ConstSizeOp op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override { ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<ConstantIndexOp>(op, op.value().getSExtValue()); rewriter.replaceOpWithNewOp<ConstantIndexOp>(op, op.value().getSExtValue());
return success(); return success();
} }

View File

@ -1804,8 +1804,8 @@ struct AllocLikeOpLowering : public ConvertOpToLLVMPattern<AllocLikeOp> {
if (!typeConverter.getOptions().useAlignedAlloc) if (!typeConverter.getOptions().useAlignedAlloc)
return None; return None;
if (allocOp.alignment()) if (Optional<uint64_t> alignment = allocOp.alignment())
return allocOp.alignment().getValue().getSExtValue(); return *alignment;
// Whenever we don't have alignment set, we will use an alignment // Whenever we don't have alignment set, we will use an alignment
// consistent with the element type; since the allocation size has to be a // consistent with the element type; since the allocation size has to be a
@ -1843,8 +1843,7 @@ struct AllocLikeOpLowering : public ConvertOpToLLVMPattern<AllocLikeOp> {
accessAlignment = nullptr; accessAlignment = nullptr;
return rewriter.create<LLVM::AllocaOp>( return rewriter.create<LLVM::AllocaOp>(
loc, elementPtrType, cumulativeSize, loc, elementPtrType, cumulativeSize,
allocaOp.alignment() ? allocaOp.alignment().getValue().getSExtValue() allocaOp.alignment() ? *allocaOp.alignment() : 0);
: 0);
} }
// Heap allocations. // Heap allocations.
@ -1892,9 +1891,8 @@ struct AllocLikeOpLowering : public ConvertOpToLLVMPattern<AllocLikeOp> {
callArgs = {alignedAllocAlignmentValue, cumulativeSize}; callArgs = {alignedAllocAlignmentValue, cumulativeSize};
} else { } else {
// Adjust the allocation size to consider alignment. // Adjust the allocation size to consider alignment.
if (allocOp.alignment()) { if (Optional<uint64_t> alignment = allocOp.alignment()) {
accessAlignment = createIndexConstant( accessAlignment = createIndexConstant(rewriter, loc, *alignment);
rewriter, loc, allocOp.alignment().getValue().getSExtValue());
cumulativeSize = rewriter.create<LLVM::SubOp>( cumulativeSize = rewriter.create<LLVM::SubOp>(
loc, loc,
rewriter.create<LLVM::AddOp>(loc, cumulativeSize, accessAlignment), rewriter.create<LLVM::AddOp>(loc, cumulativeSize, accessAlignment),
@ -2537,7 +2535,7 @@ struct PrefetchOpLowering : public LoadStoreOpLowering<PrefetchOp> {
rewriter.getI32IntegerAttr(prefetchOp.isWrite())); rewriter.getI32IntegerAttr(prefetchOp.isWrite()));
auto localityHint = rewriter.create<LLVM::ConstantOp>( auto localityHint = rewriter.create<LLVM::ConstantOp>(
op->getLoc(), llvmI32Type, op->getLoc(), llvmI32Type,
rewriter.getI32IntegerAttr(prefetchOp.localityHint().getZExtValue())); rewriter.getI32IntegerAttr(prefetchOp.localityHint()));
auto isData = rewriter.create<LLVM::ConstantOp>( auto isData = rewriter.create<LLVM::ConstantOp>(
op->getLoc(), llvmI32Type, op->getLoc(), llvmI32Type,
rewriter.getI32IntegerAttr(prefetchOp.isDataCache())); rewriter.getI32IntegerAttr(prefetchOp.isDataCache()));
@ -3063,7 +3061,7 @@ struct AssumeAlignmentOpLowering
ConversionPatternRewriter &rewriter) const override { ConversionPatternRewriter &rewriter) const override {
AssumeAlignmentOp::Adaptor transformed(operands); AssumeAlignmentOp::Adaptor transformed(operands);
Value memref = transformed.memref(); Value memref = transformed.memref();
unsigned alignment = cast<AssumeAlignmentOp>(op).alignment().getZExtValue(); unsigned alignment = cast<AssumeAlignmentOp>(op).alignment();
MemRefDescriptor memRefDescriptor(memref); MemRefDescriptor memRefDescriptor(memref);
Value ptr = memRefDescriptor.alignedPtr(rewriter, memref.getLoc()); Value ptr = memRefDescriptor.alignedPtr(rewriter, memref.getLoc());

View File

@ -878,9 +878,8 @@ void SimplifyAffineOp<AffinePrefetchOp>::replaceAffineOp(
PatternRewriter &rewriter, AffinePrefetchOp prefetch, AffineMap map, PatternRewriter &rewriter, AffinePrefetchOp prefetch, AffineMap map,
ArrayRef<Value> mapOperands) const { ArrayRef<Value> mapOperands) const {
rewriter.replaceOpWithNewOp<AffinePrefetchOp>( rewriter.replaceOpWithNewOp<AffinePrefetchOp>(
prefetch, prefetch.memref(), map, mapOperands, prefetch, prefetch.memref(), map, mapOperands, prefetch.localityHint(),
prefetch.localityHint().getZExtValue(), prefetch.isWrite(), prefetch.isWrite(), prefetch.isDataCache());
prefetch.isDataCache());
} }
template <> template <>
void SimplifyAffineOp<AffineStoreOp>::replaceAffineOp( void SimplifyAffineOp<AffineStoreOp>::replaceAffineOp(

View File

@ -124,7 +124,7 @@ static void printAllocaOp(OpAsmPrinter &p, AllocaOp &op) {
op.getContext()); op.getContext());
p << op.getOperationName() << ' ' << op.arraySize() << " x " << elemTy; p << op.getOperationName() << ' ' << op.arraySize() << " x " << elemTy;
if (op.alignment().hasValue() && op.alignment()->getSExtValue() != 0) if (op.alignment().hasValue() && *op.alignment() != 0)
p.printOptionalAttrDict(op.getAttrs()); p.printOptionalAttrDict(op.getAttrs());
else else
p.printOptionalAttrDict(op.getAttrs(), {"alignment"}); p.printOptionalAttrDict(op.getAttrs(), {"alignment"});
@ -914,9 +914,8 @@ static LogicalResult verify(AddressOfOp op) {
return op.emitOpError( return op.emitOpError(
"must reference a global defined by 'llvm.mlir.global' or 'llvm.func'"); "must reference a global defined by 'llvm.mlir.global' or 'llvm.func'");
if (global && if (global && global.getType().getPointerTo(global.addr_space()) !=
global.getType().getPointerTo(global.addr_space().getZExtValue()) != op.getResult().getType())
op.getResult().getType())
return op.emitOpError( return op.emitOpError(
"the type must be a pointer to the type of the referenced global"); "the type must be a pointer to the type of the referenced global");

View File

@ -89,9 +89,9 @@ public:
QuantizedType convertFakeQuantAttrsToType(ConstFakeQuant fqOp, QuantizedType convertFakeQuantAttrsToType(ConstFakeQuant fqOp,
Type expressedType) const { Type expressedType) const {
return fakeQuantAttrsToType( return fakeQuantAttrsToType(
fqOp.getLoc(), fqOp.num_bits().getSExtValue(), fqOp.getLoc(), fqOp.num_bits(), fqOp.min().convertToFloat(),
fqOp.min().convertToFloat(), fqOp.max().convertToFloat(), fqOp.max().convertToFloat(), fqOp.narrow_range(), expressedType,
fqOp.narrow_range(), expressedType, fqOp.is_signed()); fqOp.is_signed());
} }
}; };
@ -115,9 +115,8 @@ public:
for (auto m : fqOp.max()) for (auto m : fqOp.max())
max.push_back(m.cast<FloatAttr>().getValueAsDouble()); max.push_back(m.cast<FloatAttr>().getValueAsDouble());
return fakeQuantAttrsToType(fqOp.getLoc(), fqOp.num_bits().getSExtValue(), return fakeQuantAttrsToType(fqOp.getLoc(), fqOp.num_bits(), fqOp.axis(),
fqOp.axis().getSExtValue(), min, max, min, max, fqOp.narrow_range(), expressedType,
fqOp.narrow_range(), expressedType,
fqOp.is_signed()); fqOp.is_signed());
} }
}; };

View File

@ -250,7 +250,7 @@ static void printMemoryAccessAttribute(
MemoryOpTy memoryOp, OpAsmPrinter &printer, MemoryOpTy memoryOp, OpAsmPrinter &printer,
SmallVectorImpl<StringRef> &elidedAttrs, SmallVectorImpl<StringRef> &elidedAttrs,
Optional<spirv::MemoryAccess> memoryAccessAtrrValue = None, Optional<spirv::MemoryAccess> memoryAccessAtrrValue = None,
Optional<llvm::APInt> alignmentAttrValue = None) { Optional<uint32_t> alignmentAttrValue = None) {
// Print optional memory access attribute. // Print optional memory access attribute.
if (auto memAccess = (memoryAccessAtrrValue ? memoryAccessAtrrValue if (auto memAccess = (memoryAccessAtrrValue ? memoryAccessAtrrValue
: memoryOp.memory_access())) { : memoryOp.memory_access())) {
@ -280,7 +280,7 @@ static void printSourceMemoryAccessAttribute(
MemoryOpTy memoryOp, OpAsmPrinter &printer, MemoryOpTy memoryOp, OpAsmPrinter &printer,
SmallVectorImpl<StringRef> &elidedAttrs, SmallVectorImpl<StringRef> &elidedAttrs,
Optional<spirv::MemoryAccess> memoryAccessAtrrValue = None, Optional<spirv::MemoryAccess> memoryAccessAtrrValue = None,
Optional<llvm::APInt> alignmentAttrValue = None) { Optional<uint32_t> alignmentAttrValue = None) {
printer << ", "; printer << ", ";

View File

@ -468,7 +468,7 @@ void AssertOp::getCanonicalizationPatterns(OwningRewritePatternList &patterns,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
static LogicalResult verify(AssumeAlignmentOp op) { static LogicalResult verify(AssumeAlignmentOp op) {
unsigned alignment = op.alignment().getZExtValue(); unsigned alignment = op.alignment();
if (!llvm::isPowerOf2_32(alignment)) if (!llvm::isPowerOf2_32(alignment))
return op.emitOpError("alignment must be power of 2"); return op.emitOpError("alignment must be power of 2");
return success(); return success();

View File

@ -721,7 +721,7 @@ LogicalResult ModuleTranslation::convertGlobals() {
((linkage == llvm::GlobalVariable::ExternalLinkage && ((linkage == llvm::GlobalVariable::ExternalLinkage &&
isa<llvm::UndefValue>(cst)) || isa<llvm::UndefValue>(cst)) ||
linkage == llvm::GlobalVariable::ExternalWeakLinkage); linkage == llvm::GlobalVariable::ExternalWeakLinkage);
auto addrSpace = op.addr_space().getLimitedValue(); auto addrSpace = op.addr_space();
auto *var = new llvm::GlobalVariable( auto *var = new llvm::GlobalVariable(
*llvmModule, type, op.constant(), linkage, *llvmModule, type, op.constant(), linkage,
anyExternalLinkage ? nullptr : cst, op.sym_name(), anyExternalLinkage ? nullptr : cst, op.sym_name(),

View File

@ -62,13 +62,6 @@ func @alloca_non_function_type() {
// ----- // -----
func @alloca_nonpositive_alignment(%size : !llvm.i64) {
// expected-error@+1 {{expected positive alignment}}
llvm.alloca %size x !llvm.i32 {alignment = -1} : (!llvm.i64) -> (!llvm.ptr<i32>)
}
// -----
func @gep_missing_input_result_type(%pos : !llvm.i64, %base : !llvm.ptr<float>) { func @gep_missing_input_result_type(%pos : !llvm.i64, %base : !llvm.ptr<float>) {
// expected-error@+1 {{2 operands present, but expected 0}} // expected-error@+1 {{2 operands present, but expected 0}}
llvm.getelementptr %base[%pos] : () -> () llvm.getelementptr %base[%pos] : () -> ()

View File

@ -457,8 +457,7 @@ struct TestBoundedRecursiveRewrite
PatternRewriter &rewriter) const final { PatternRewriter &rewriter) const final {
// Decrement the depth of the op in-place. // Decrement the depth of the op in-place.
rewriter.updateRootInPlace(op, [&] { rewriter.updateRootInPlace(op, [&] {
op.setAttr("depth", op.setAttr("depth", rewriter.getI64IntegerAttr(op.depth() - 1));
rewriter.getI64IntegerAttr(op.depth().getSExtValue() - 1));
}); });
return success(); return success();
} }

View File

@ -138,8 +138,8 @@ def BOp : NS_Op<"b_op", []> {
// DEF: ::mlir::Attribute BOp::any_attr() // DEF: ::mlir::Attribute BOp::any_attr()
// DEF: bool BOp::bool_attr() // DEF: bool BOp::bool_attr()
// DEF: ::llvm::APInt BOp::i32_attr() // DEF: uint32_t BOp::i32_attr()
// DEF: ::llvm::APInt BOp::i64_attr() // DEF: uint64_t BOp::i64_attr()
// DEF: ::llvm::APFloat BOp::f32_attr() // DEF: ::llvm::APFloat BOp::f32_attr()
// DEF: ::llvm::APFloat BOp::f64_attr() // DEF: ::llvm::APFloat BOp::f64_attr()
// DEF: ::llvm::StringRef BOp::str_attr() // DEF: ::llvm::StringRef BOp::str_attr()
@ -196,7 +196,7 @@ def DOp : NS_Op<"d_op", []> {
} }
// DECL-LABEL: DOp declarations // DECL-LABEL: DOp declarations
// DECL: static void build({{.*}}, ::llvm::APInt i32_attr, ::llvm::APFloat f64_attr, ::llvm::StringRef str_attr, bool bool_attr, ::SomeI32Enum enum_attr, ::llvm::APInt dv_i32_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef dv_str_attr = "abc", bool dv_bool_attr = true, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5) // DECL: static void build({{.*}}, uint32_t i32_attr, ::llvm::APFloat f64_attr, ::llvm::StringRef str_attr, bool bool_attr, ::SomeI32Enum enum_attr, uint32_t dv_i32_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef dv_str_attr = "abc", bool dv_bool_attr = true, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5)
// DEF-LABEL: DOp definitions // DEF-LABEL: DOp definitions
// DEF: odsState.addAttribute("str_attr", odsBuilder.getStringAttr(str_attr)); // DEF: odsState.addAttribute("str_attr", odsBuilder.getStringAttr(str_attr));
@ -238,7 +238,7 @@ def EOp : NS_Op<"e_op", []> {
} }
// DECL-LABEL: EOp declarations // DECL-LABEL: EOp declarations
// DECL: static void build({{.*}}, ::llvm::APInt i32_attr, ::llvm::APInt dv_i32_attr, ::llvm::APFloat f64_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef str_attr, ::llvm::StringRef dv_str_attr, bool bool_attr, bool dv_bool_attr, ::SomeI32Enum enum_attr, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5) // DECL: static void build({{.*}}, uint32_t i32_attr, uint32_t dv_i32_attr, ::llvm::APFloat f64_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef str_attr, ::llvm::StringRef dv_str_attr, bool bool_attr, bool dv_bool_attr, ::SomeI32Enum enum_attr, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5)
// Test mixing operands and attributes in arbitrary order // Test mixing operands and attributes in arbitrary order
// --- // ---

View File

@ -77,12 +77,12 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {
// CHECK: ::mlir::Region &someRegion(); // CHECK: ::mlir::Region &someRegion();
// CHECK: ::mlir::MutableArrayRef<Region> someRegions(); // CHECK: ::mlir::MutableArrayRef<Region> someRegions();
// CHECK: ::mlir::IntegerAttr attr1Attr() // CHECK: ::mlir::IntegerAttr attr1Attr()
// CHECK: ::llvm::APInt attr1(); // CHECK: uint32_t attr1();
// CHECK: ::mlir::FloatAttr attr2Attr() // CHECK: ::mlir::FloatAttr attr2Attr()
// CHECK: ::llvm::Optional< ::llvm::APFloat > attr2(); // CHECK: ::llvm::Optional< ::llvm::APFloat > attr2();
// CHECK: static void build(Value val); // CHECK: static void build(Value val);
// CHECK: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type r, ::llvm::ArrayRef<::mlir::Type> s, ::mlir::Value a, ::mlir::ValueRange b, ::mlir::IntegerAttr attr1, /*optional*/::mlir::FloatAttr attr2, unsigned someRegionsCount) // CHECK: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type r, ::llvm::ArrayRef<::mlir::Type> s, ::mlir::Value a, ::mlir::ValueRange b, ::mlir::IntegerAttr attr1, /*optional*/::mlir::FloatAttr attr2, unsigned someRegionsCount)
// CHECK: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type r, ::llvm::ArrayRef<::mlir::Type> s, ::mlir::Value a, ::mlir::ValueRange b, ::llvm::APInt attr1, /*optional*/::mlir::FloatAttr attr2, unsigned someRegionsCount) // CHECK: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type r, ::llvm::ArrayRef<::mlir::Type> s, ::mlir::Value a, ::mlir::ValueRange b, uint32_t attr1, /*optional*/::mlir::FloatAttr attr2, unsigned someRegionsCount)
// CHECK: static void build(::mlir::OpBuilder &, ::mlir::OperationState &odsState, ::llvm::ArrayRef<::mlir::Type> resultTypes, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes, unsigned numRegions) // CHECK: static void build(::mlir::OpBuilder &, ::mlir::OperationState &odsState, ::llvm::ArrayRef<::mlir::Type> resultTypes, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes, unsigned numRegions)
// CHECK: static ::mlir::ParseResult parse(::mlir::OpAsmParser &parser, ::mlir::OperationState &result); // CHECK: static ::mlir::ParseResult parse(::mlir::OpAsmParser &parser, ::mlir::OperationState &result);
// CHECK: void print(::mlir::OpAsmPrinter &p); // CHECK: void print(::mlir::OpAsmPrinter &p);