xref: /llvm-project/mlir/lib/Conversion/LLVMCommon/StructBuilder.cpp (revision d0f19ce774b0d3fa5cd3b22561cc75c7b38b290d)
1b5d847b1SAlex Zinenko //===- StructBuilder.cpp - Helper for building LLVM structs  --------------===//
2b5d847b1SAlex Zinenko //
3b5d847b1SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b5d847b1SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5b5d847b1SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b5d847b1SAlex Zinenko //
7b5d847b1SAlex Zinenko //===----------------------------------------------------------------------===//
8b5d847b1SAlex Zinenko 
9b5d847b1SAlex Zinenko #include "mlir/Conversion/LLVMCommon/StructBuilder.h"
10b5d847b1SAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11b5d847b1SAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
12b5d847b1SAlex Zinenko #include "mlir/IR/Builders.h"
13b5d847b1SAlex Zinenko 
14b5d847b1SAlex Zinenko using namespace mlir;
15b5d847b1SAlex Zinenko 
16b5d847b1SAlex Zinenko //===----------------------------------------------------------------------===//
17b5d847b1SAlex Zinenko // StructBuilder implementation
18b5d847b1SAlex Zinenko //===----------------------------------------------------------------------===//
19b5d847b1SAlex Zinenko 
StructBuilder(Value v)20b5d847b1SAlex Zinenko StructBuilder::StructBuilder(Value v) : value(v), structType(v.getType()) {
21b5d847b1SAlex Zinenko   assert(value != nullptr && "value cannot be null");
22b5d847b1SAlex Zinenko   assert(LLVM::isCompatibleType(structType) && "expected llvm type");
23b5d847b1SAlex Zinenko }
24b5d847b1SAlex Zinenko 
extractPtr(OpBuilder & builder,Location loc,unsigned pos) const25b5d847b1SAlex Zinenko Value StructBuilder::extractPtr(OpBuilder &builder, Location loc,
26*d0f19ce7SKrzysztof Drewniak                                 unsigned pos) const {
275c5af910SJeff Niu   return builder.create<LLVM::ExtractValueOp>(loc, value, pos);
28b5d847b1SAlex Zinenko }
29b5d847b1SAlex Zinenko 
setPtr(OpBuilder & builder,Location loc,unsigned pos,Value ptr)30b5d847b1SAlex Zinenko void StructBuilder::setPtr(OpBuilder &builder, Location loc, unsigned pos,
31b5d847b1SAlex Zinenko                            Value ptr) {
325c5af910SJeff Niu   value = builder.create<LLVM::InsertValueOp>(loc, value, ptr, pos);
33b5d847b1SAlex Zinenko }
34