34 Type llvmType = containerType;
36 emitError(
"expected LLVM IR Dialect type, got ") << containerType;
45 if (
auto arrayType = llvm::dyn_cast<LLVMArrayType>(llvmType)) {
46 if (idx < 0 ||
static_cast<unsigned>(idx) >= arrayType.getNumElements()) {
47 emitError(
"position out of bounds: ") << idx;
50 llvmType = arrayType.getElementType();
51 }
else if (
auto structType = llvm::dyn_cast<LLVMStructType>(llvmType)) {
53 static_cast<unsigned>(idx) >= structType.getBody().size()) {
54 emitError(
"position out of bounds: ") << idx;
57 llvmType = structType.getBody()[idx];
59 emitError(
"expected LLVM IR structure/array type, got: ") << llvmType;
71 if (
auto structType = llvm::dyn_cast<LLVMStructType>(llvmType))
72 llvmType = structType.getBody()[idx];
74 llvmType = llvm::cast<LLVMArrayType>(llvmType).getElementType();
86 if (
auto elementsAttr = dyn_cast<ElementsAttr>(attr)) {
87 ShapedType shapedType = elementsAttr.getShapedType();
88 if (!shapedType.hasRank() || shapedType.getRank() != 1)
90 if (
index <
static_cast<size_t>(elementsAttr.getNumElements()))
94 if (
auto arrayAttr = dyn_cast<ArrayAttr>(attr)) {
95 if (
index < arrayAttr.getValue().size())
96 return arrayAttr[
index];
99 if (isa<ZeroAttr, UndefAttr, PoisonAttr>(attr))
104OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
105 if (
auto extractValueOp = getContainer().getDefiningOp<ExtractValueOp>()) {
107 newPos.append(getPosition().begin(), getPosition().end());
109 getContainerMutable().set(extractValueOp.getContainer());
115 for (
int64_t pos : getPosition()) {
120 return containerAttr;
123 Value container = getContainer();
125 while (
auto insertValueOp = container.
getDefiningOp<InsertValueOp>()) {
127 auto extractPosSize = extractPos.size();
128 auto insertPosSize = insertPos.size();
131 if (extractPos == insertPos)
132 return insertValueOp.getValue();
146 if (extractPosSize > insertPosSize &&
147 extractPos.take_front(insertPosSize) == insertPos) {
148 container = insertValueOp.getValue();
149 extractPos = extractPos.drop_front(insertPosSize);
165 if (insertPosSize > extractPosSize &&
166 extractPos == insertPos.take_front(extractPosSize))
171 container = insertValueOp.getContainer();
177 if (container == getContainer())
179 setPosition(extractPos);
180 getContainerMutable().assign(container);
184LogicalResult ExtractValueOp::verify() {
185 auto emitError = [
this](StringRef msg) {
return emitOpError(msg); };
191 if (getRes().
getType() != valueType)
192 return emitOpError() <<
"Type mismatch: extracting from "
193 << getContainer().getType() <<
" should produce "
194 << valueType <<
" but this op returns "
195 << getRes().getType();
201 build(builder, state,
240 LogicalResult matchAndRewrite(InsertValueOp insertOp,
241 PatternRewriter &rewriter)
const override {
242 bool changed =
false;
248 auto insertBaseIdx = insertOp.getPosition()[0];
249 for (
auto &use : insertOp->getUses()) {
250 if (
auto extractOp = dyn_cast<ExtractValueOp>(use.getOwner())) {
251 auto baseIdx = extractOp.getPosition()[0];
254 if (baseIdx == insertBaseIdx)
256 posToExtractOps[baseIdx].push_back(extractOp);
261 Value nextContainer = insertOp.getContainer();
262 while (!posToExtractOps.empty()) {
264 dyn_cast_or_null<InsertValueOp>(nextContainer.
getDefiningOp());
267 nextContainer = curInsert.getContainer();
270 auto curInsertBaseIdx = curInsert.getPosition()[0];
271 auto it = posToExtractOps.find(curInsertBaseIdx);
272 if (it == posToExtractOps.end())
276 for (
auto &extractOp : it->second) {
278 extractOp.getContainerMutable().assign(curInsert);
283 assert(!it->second.empty());
285 posToExtractOps.erase(it);
289 for (
auto &[baseIdx, extracts] : posToExtractOps) {
290 for (
auto &extractOp : extracts) {
292 extractOp.getContainerMutable().assign(nextContainer);
295 assert(!extracts.empty() &&
"Empty list in map");
305 patterns.
add<ResolveExtractValueSource>(context);
325LogicalResult InsertValueOp::verify() {
326 auto emitError = [
this](StringRef msg) {
return emitOpError(msg); };
332 if (getValue().
getType() != valueType)
333 return emitOpError() <<
"Type mismatch: cannot insert "
334 << getValue().getType() <<
" into "
335 << getContainer().getType();
340#define GET_OP_CLASSES
341#include "mlir/Dialect/LLVMIR/LLVMAggregateOps.cpp.inc"
static Type getInsertExtractValueElementType(function_ref< InFlightDiagnostic(StringRef)> emitError, Type containerType, ArrayRef< int64_t > position)
Extract the type at position in the LLVM IR aggregate type containerType.
static Attribute extractElementAt(Attribute attr, size_t index)
Extracts the element at the given index from an attribute.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
ArrayRef< T > asArrayRef() const
mlir::ParseResult parseInsertExtractValueElementType(mlir::AsmParser &parser, mlir::Type &valueType, mlir::Type containerType, mlir::DenseI64ArrayAttr position)
Infer the value type from the container type and position.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
void printInsertExtractValueElementType(mlir::AsmPrinter &printer, mlir::Operation *op, mlir::Type valueType, mlir::Type containerType, mlir::DenseI64ArrayAttr position)
Nothing to print for an inferred type.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
llvm::function_ref< Fn > function_ref
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
This represents an operation in an abstracted form, suitable for use with the builder APIs.