MLIR  14.0.0git
StructBuilder.cpp
Go to the documentation of this file.
1 //===- StructBuilder.cpp - Helper for building LLVM structs --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
12 #include "mlir/IR/Builders.h"
13 
14 using namespace mlir;
15 
16 //===----------------------------------------------------------------------===//
17 // StructBuilder implementation
18 //===----------------------------------------------------------------------===//
19 
20 StructBuilder::StructBuilder(Value v) : value(v), structType(v.getType()) {
21  assert(value != nullptr && "value cannot be null");
22  assert(LLVM::isCompatibleType(structType) && "expected llvm type");
23 }
24 
26  unsigned pos) {
27  Type type = structType.cast<LLVM::LLVMStructType>().getBody()[pos];
28  return builder.create<LLVM::ExtractValueOp>(loc, type, value,
29  builder.getI64ArrayAttr(pos));
30 }
31 
32 void StructBuilder::setPtr(OpBuilder &builder, Location loc, unsigned pos,
33  Value ptr) {
34  value = builder.create<LLVM::InsertValueOp>(loc, structType, value, ptr,
35  builder.getI64ArrayAttr(pos));
36 }
Include the generated interface declarations.
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
Definition: Builders.h:430
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:220
static constexpr const bool value
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:752
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:84
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
Definition: LLVMTypes.h:252
StructBuilder(Value v)
Construct a helper for the given value.
Value extractPtr(OpBuilder &builder, Location loc, unsigned pos)
Builds IR to extract a value from the struct at position pos.
void setPtr(OpBuilder &builder, Location loc, unsigned pos, Value ptr)
Builds IR to set a value in the struct at position pos.
This class helps build Operations.
Definition: Builders.h:177
U cast() const
Definition: Types.h:250