github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/EDSC/Intrinsics.cpp (about)

     1  //===- Intrinsics.cpp - MLIR Operations for Declarative Builders ----------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  
    18  #include "mlir/EDSC/Intrinsics.h"
    19  #include "mlir/Dialect/VectorOps/VectorOps.h"
    20  #include "mlir/EDSC/Builders.h"
    21  #include "mlir/IR/AffineExpr.h"
    22  
    23  using namespace mlir;
    24  using namespace mlir::edsc;
    25  
    26  OperationHandle mlir::edsc::intrinsics::br(BlockHandle bh,
    27                                             ArrayRef<ValueHandle> operands) {
    28    assert(bh && "Expected already captured BlockHandle");
    29    for (auto &o : operands) {
    30      (void)o;
    31      assert(o && "Expected already captured ValueHandle");
    32    }
    33    SmallVector<Value *, 4> ops(operands.begin(), operands.end());
    34    return OperationHandle::create<BranchOp>(bh.getBlock(), ops);
    35  }
    36  static void enforceEmptyCapturesMatchOperands(ArrayRef<ValueHandle *> captures,
    37                                                ArrayRef<ValueHandle> operands) {
    38    assert(captures.size() == operands.size() &&
    39           "Expected same number of captures as operands");
    40    for (auto it : llvm::zip(captures, operands)) {
    41      (void)it;
    42      assert(!std::get<0>(it)->hasValue() &&
    43             "Unexpected already captured ValueHandle");
    44      assert(std::get<1>(it) && "Expected already captured ValueHandle");
    45      assert(std::get<0>(it)->getType() == std::get<1>(it).getType() &&
    46             "Expected the same type for capture and operand");
    47    }
    48  }
    49  
    50  OperationHandle mlir::edsc::intrinsics::br(BlockHandle *bh,
    51                                             ArrayRef<ValueHandle *> captures,
    52                                             ArrayRef<ValueHandle> operands) {
    53    assert(!*bh && "Unexpected already captured BlockHandle");
    54    enforceEmptyCapturesMatchOperands(captures, operands);
    55    BlockBuilder(bh, captures)(/* no body */);
    56    SmallVector<Value *, 4> ops(operands.begin(), operands.end());
    57    return OperationHandle::create<BranchOp>(bh->getBlock(), ops);
    58  }
    59  
    60  OperationHandle
    61  mlir::edsc::intrinsics::cond_br(ValueHandle cond, BlockHandle trueBranch,
    62                                  ArrayRef<ValueHandle> trueOperands,
    63                                  BlockHandle falseBranch,
    64                                  ArrayRef<ValueHandle> falseOperands) {
    65    SmallVector<Value *, 4> trueOps(trueOperands.begin(), trueOperands.end());
    66    SmallVector<Value *, 4> falseOps(falseOperands.begin(), falseOperands.end());
    67    return OperationHandle::create<CondBranchOp>(
    68        cond, trueBranch.getBlock(), trueOps, falseBranch.getBlock(), falseOps);
    69  }
    70  
    71  OperationHandle mlir::edsc::intrinsics::cond_br(
    72      ValueHandle cond, BlockHandle *trueBranch,
    73      ArrayRef<ValueHandle *> trueCaptures, ArrayRef<ValueHandle> trueOperands,
    74      BlockHandle *falseBranch, ArrayRef<ValueHandle *> falseCaptures,
    75      ArrayRef<ValueHandle> falseOperands) {
    76    assert(!*trueBranch && "Unexpected already captured BlockHandle");
    77    assert(!*falseBranch && "Unexpected already captured BlockHandle");
    78    enforceEmptyCapturesMatchOperands(trueCaptures, trueOperands);
    79    enforceEmptyCapturesMatchOperands(falseCaptures, falseOperands);
    80    BlockBuilder(trueBranch, trueCaptures)(/* no body */);
    81    BlockBuilder(falseBranch, falseCaptures)(/* no body */);
    82    SmallVector<Value *, 4> trueOps(trueOperands.begin(), trueOperands.end());
    83    SmallVector<Value *, 4> falseOps(falseOperands.begin(), falseOperands.end());
    84    return OperationHandle::create<CondBranchOp>(
    85        cond, trueBranch->getBlock(), trueOps, falseBranch->getBlock(), falseOps);
    86  }