Skip to content

Commit 431ea5b

Browse files
Iterators: Use new iterator states in IteratorsToLLVM. (#608)
1 parent 31a2196 commit 431ea5b

File tree

16 files changed

+288
-330
lines changed

16 files changed

+288
-330
lines changed

experimental/iterators/lib/Conversion/IteratorsToLLVM/IteratorAnalysis.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
#include "iterators/Dialect/Iterators/IR/Iterators.h"
44
#include "iterators/Utils/NameAssigner.h"
5-
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
65
#include "mlir/IR/BuiltinAttributes.h"
76
#include "mlir/Transforms/DialectConversion.h"
87
#include "llvm/ADT/TypeSwitch.h"
98

109
using namespace mlir;
1110
using namespace mlir::iterators;
12-
using namespace LLVM;
1311

1412
using SymbolTriple = std::tuple<SymbolRefAttr, SymbolRefAttr, SymbolRefAttr>;
1513

@@ -52,8 +50,8 @@ class StateTypeComputer {
5250
/// Computes the state type of the given op whose upstream iterator ops have
5351
/// the state types given in upstreamStateTypes.
5452
template <typename OpType>
55-
LLVMStructType
56-
operator()(OpType op, llvm::SmallVector<LLVMStructType> upstreamStateTypes);
53+
StateType operator()(OpType op,
54+
llvm::SmallVector<StateType> upstreamStateTypes);
5755

5856
private:
5957
TypeConverter typeConverter;
@@ -62,41 +60,41 @@ class StateTypeComputer {
6260
/// The state of ConstantStreamOp consists of a single number that corresponds
6361
/// to the index of the next struct returned by the iterator.
6462
template <>
65-
LLVMStructType StateTypeComputer::operator()(
66-
ConstantStreamOp op,
67-
llvm::SmallVector<LLVMStructType> /*upstreamStateTypes*/) {
63+
StateType StateTypeComputer::operator()(
64+
ConstantStreamOp op, llvm::SmallVector<StateType> /*upstreamStateTypes*/) {
6865
MLIRContext *context = op->getContext();
6966
Type i32 = IntegerType::get(context, /*width=*/32);
70-
return LLVMStructType::getNewIdentified(
71-
context, "iterators.constant_stream_state", {i32});
67+
return StateType::get(context, {i32});
7268
}
7369

7470
/// The state of FilterOp only consists of the state of its upstream iterator,
7571
/// i.e., the state of the iterator that produces its input stream.
7672
template <>
77-
LLVMStructType StateTypeComputer::operator()(
78-
FilterOp op, llvm::SmallVector<LLVMStructType> upstreamStateTypes) {
79-
return LLVMStructType::getNewIdentified(
80-
op->getContext(), "iterators.filter_state", {upstreamStateTypes[0]});
73+
StateType
74+
StateTypeComputer::operator()(FilterOp op,
75+
llvm::SmallVector<StateType> upstreamStateTypes) {
76+
MLIRContext *context = op->getContext();
77+
return StateType::get(context, {upstreamStateTypes[0]});
8178
}
8279

8380
/// The state of MapOp only consists of the state of its upstream iterator,
8481
/// i.e., the state of the iterator that produces its input stream.
8582
template <>
86-
LLVMStructType StateTypeComputer::operator()(
87-
MapOp op, llvm::SmallVector<LLVMStructType> upstreamStateTypes) {
88-
return LLVMStructType::getNewIdentified(
89-
op->getContext(), "iterators.map_state", {upstreamStateTypes[0]});
83+
StateType
84+
StateTypeComputer::operator()(MapOp op,
85+
llvm::SmallVector<StateType> upstreamStateTypes) {
86+
MLIRContext *context = op->getContext();
87+
return StateType::get(context, {upstreamStateTypes[0]});
9088
}
9189

9290
/// The state of ReduceOp only consists of the state of its upstream iterator,
9391
/// i.e., the state of the iterator that produces its input stream.
9492
template <>
95-
LLVMStructType StateTypeComputer::operator()(
96-
ReduceOp op, llvm::SmallVector<LLVMStructType> upstreamStateTypes) {
97-
assert(upstreamStateTypes.size() == 1);
98-
return LLVMStructType::getNewIdentified(
99-
op->getContext(), "iterators.reduce_state", {upstreamStateTypes[0]});
93+
StateType
94+
StateTypeComputer::operator()(ReduceOp op,
95+
llvm::SmallVector<StateType> upstreamStateTypes) {
96+
MLIRContext *context = op->getContext();
97+
return StateType::get(context, {upstreamStateTypes[0]});
10098
}
10199

102100
/// The state of TabularViewToStreamOp consists of a single number that
@@ -106,23 +104,21 @@ LLVMStructType StateTypeComputer::operator()(
106104
/// template <typename TabularViewType>
107105
/// struct { int64_t currentIndex; TabularViewType view; }
108106
template <>
109-
LLVMStructType StateTypeComputer::operator()(
107+
StateType StateTypeComputer::operator()(
110108
TabularViewToStreamOp op,
111-
llvm::SmallVector<LLVM::LLVMStructType> /*upstreamStateTypes*/) {
109+
llvm::SmallVector<StateType> /*upstreamStateTypes*/) {
112110
MLIRContext *context = op->getContext();
113-
Type i64 = IntegerType::get(context, /*width=*/64);
111+
Type indexType = IntegerType::get(context, /*width=*/64);
114112
Type viewType = typeConverter.convertType(op.input().getType());
115-
return LLVM::LLVMStructType::getNewIdentified(
116-
op->getContext(), "iterators.tabular_view_to_stream_state",
117-
{i64, viewType});
113+
return StateType::get(context, {indexType, viewType});
118114
}
119115

120116
/// Build IteratorInfo, assigning new unique names as needed. Takes the
121-
/// `LLVMStructType` as a parameter, to ensure proper build order (all uses are
117+
/// `StateType` as a parameter, to ensure proper build order (all uses are
122118
/// visited before any def).
123119
mlir::iterators::IteratorInfo::IteratorInfo(IteratorOpInterface op,
124120
NameAssigner &nameAssigner,
125-
LLVMStructType t) {
121+
StateType t) {
126122
std::tie(openFunc, nextFunc, closeFunc) =
127123
assignFunctionNames(op, nameAssigner);
128124
stateType = t;
@@ -166,16 +162,16 @@ mlir::iterators::IteratorAnalysis::IteratorAnalysis(
166162
TabularViewToStreamOp
167163
// clang-format on
168164
>([&](auto op) {
169-
llvm::SmallVector<LLVMStructType> upstreamStateTypes;
165+
llvm::SmallVector<StateType> upstreamStateTypes;
170166
llvm::transform(op->getOperands(),
171167
std::back_inserter(upstreamStateTypes),
172168
[&](auto operand) {
173169
Operation *def = operand.getDefiningOp();
174170
if (!def || !llvm::isa<IteratorOpInterface>(def))
175-
return LLVMStructType();
171+
return StateType();
176172
return getExpectedIteratorInfo(def).stateType;
177173
});
178-
LLVMStructType stateType = stateTypeComputer(op, upstreamStateTypes);
174+
StateType stateType = stateTypeComputer(op, upstreamStateTypes);
179175
setIteratorInfo(op, IteratorInfo(op, nameAssigner, stateType));
180176
})
181177
.Default([&](auto op) { assert(false && "Unexpected op"); });

experimental/iterators/lib/Conversion/IteratorsToLLVM/IteratorAnalysis.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef EXPERIMENTAL_ITERATORS_LIB_CONVERSION_ITERATORSTOLLVM_ITERATORANALYSIS_H
1010
#define EXPERIMENTAL_ITERATORS_LIB_CONVERSION_ITERATORSTOLLVM_ITERATORANALYSIS_H
1111

12+
#include "iterators/Dialect/Iterators/IR/Iterators.h"
1213
#include "iterators/Utils/NameAssigner.h"
13-
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1414
#include "mlir/IR/BuiltinAttributes.h"
1515
namespace mlir {
1616
class ModuleOp;
@@ -23,10 +23,9 @@ class IteratorOpInterface;
2323

2424
/// Information about each iterator op constructed by IteratorAnalysis.
2525
struct IteratorInfo {
26-
/// Takes the `LLVM::LLVMStructType` as a parameter, to ensure proper build
27-
/// order (all uses are visited before any def).
28-
IteratorInfo(IteratorOpInterface op, NameAssigner &nameAssigner,
29-
LLVM::LLVMStructType t);
26+
/// Takes the `StateType` as a parameter, to ensure proper build order (all
27+
/// uses are visited before any def).
28+
IteratorInfo(IteratorOpInterface op, NameAssigner &nameAssigner, StateType t);
3029

3130
// Rule of five: default constructors/assignment operators
3231
IteratorInfo() = default;
@@ -42,7 +41,7 @@ struct IteratorInfo {
4241
SymbolRefAttr closeFunc;
4342

4443
/// State of the iterator including the state of its potential upstreams.
45-
LLVM::LLVMStructType stateType;
44+
StateType stateType;
4645
};
4746

4847
/// Constructs information about the state type and the Open/Next/Close

0 commit comments

Comments
 (0)