xref: /llvm-project/mlir/lib/Conversion/LLVMCommon/StructBuilder.cpp (revision d0f19ce774b0d3fa5cd3b22561cc75c7b38b290d)
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 
9 #include "mlir/Conversion/LLVMCommon/StructBuilder.h"
10 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
12 #include "mlir/IR/Builders.h"
13 
14 using namespace mlir;
15 
16 //===----------------------------------------------------------------------===//
17 // StructBuilder implementation
18 //===----------------------------------------------------------------------===//
19 
StructBuilder(Value v)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 
extractPtr(OpBuilder & builder,Location loc,unsigned pos) const25 Value StructBuilder::extractPtr(OpBuilder &builder, Location loc,
26                                 unsigned pos) const {
27   return builder.create<LLVM::ExtractValueOp>(loc, value, pos);
28 }
29 
setPtr(OpBuilder & builder,Location loc,unsigned pos,Value ptr)30 void StructBuilder::setPtr(OpBuilder &builder, Location loc, unsigned pos,
31                            Value ptr) {
32   value = builder.create<LLVM::InsertValueOp>(loc, value, ptr, pos);
33 }
34