summaryrefslogtreecommitdiff
path: root/mlir/lib/EDSC/Intrinsics.cpp
blob: c6738c429937a718d5f8dda0ea7128787957bcfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===- Intrinsics.cpp - MLIR Operations for Declarative Builders ----------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

#include "mlir/EDSC/Intrinsics.h"
#include "mlir/EDSC/Builders.h"
#include "mlir/IR/AffineExpr.h"

using namespace mlir;
using namespace mlir::edsc;

OperationHandle mlir::edsc::intrinsics::br(BlockHandle bh,
                                           ArrayRef<ValueHandle> operands) {
  assert(bh && "Expected already captured BlockHandle");
  for (auto &o : operands) {
    (void)o;
    assert(o && "Expected already captured ValueHandle");
  }
  SmallVector<ValuePtr, 4> ops(operands.begin(), operands.end());
  return OperationHandle::create<BranchOp>(bh.getBlock(), ops);
}
static void enforceEmptyCapturesMatchOperands(ArrayRef<ValueHandle *> captures,
                                              ArrayRef<ValueHandle> operands) {
  assert(captures.size() == operands.size() &&
         "Expected same number of captures as operands");
  for (auto it : llvm::zip(captures, operands)) {
    (void)it;
    assert(!std::get<0>(it)->hasValue() &&
           "Unexpected already captured ValueHandle");
    assert(std::get<1>(it) && "Expected already captured ValueHandle");
    assert(std::get<0>(it)->getType() == std::get<1>(it).getType() &&
           "Expected the same type for capture and operand");
  }
}

OperationHandle mlir::edsc::intrinsics::br(BlockHandle *bh,
                                           ArrayRef<ValueHandle *> captures,
                                           ArrayRef<ValueHandle> operands) {
  assert(!*bh && "Unexpected already captured BlockHandle");
  enforceEmptyCapturesMatchOperands(captures, operands);
  BlockBuilder(bh, captures)(/* no body */);
  SmallVector<ValuePtr, 4> ops(operands.begin(), operands.end());
  return OperationHandle::create<BranchOp>(bh->getBlock(), ops);
}

OperationHandle
mlir::edsc::intrinsics::cond_br(ValueHandle cond, BlockHandle trueBranch,
                                ArrayRef<ValueHandle> trueOperands,
                                BlockHandle falseBranch,
                                ArrayRef<ValueHandle> falseOperands) {
  SmallVector<ValuePtr, 4> trueOps(trueOperands.begin(), trueOperands.end());
  SmallVector<ValuePtr, 4> falseOps(falseOperands.begin(), falseOperands.end());
  return OperationHandle::create<CondBranchOp>(
      cond, trueBranch.getBlock(), trueOps, falseBranch.getBlock(), falseOps);
}

OperationHandle mlir::edsc::intrinsics::cond_br(
    ValueHandle cond, BlockHandle *trueBranch,
    ArrayRef<ValueHandle *> trueCaptures, ArrayRef<ValueHandle> trueOperands,
    BlockHandle *falseBranch, ArrayRef<ValueHandle *> falseCaptures,
    ArrayRef<ValueHandle> falseOperands) {
  assert(!*trueBranch && "Unexpected already captured BlockHandle");
  assert(!*falseBranch && "Unexpected already captured BlockHandle");
  enforceEmptyCapturesMatchOperands(trueCaptures, trueOperands);
  enforceEmptyCapturesMatchOperands(falseCaptures, falseOperands);
  BlockBuilder(trueBranch, trueCaptures)(/* no body */);
  BlockBuilder(falseBranch, falseCaptures)(/* no body */);
  SmallVector<ValuePtr, 4> trueOps(trueOperands.begin(), trueOperands.end());
  SmallVector<ValuePtr, 4> falseOps(falseOperands.begin(), falseOperands.end());
  return OperationHandle::create<CondBranchOp>(
      cond, trueBranch->getBlock(), trueOps, falseBranch->getBlock(), falseOps);
}