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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
//===- IndexToSPIRV.cpp - Index to SPIRV dialect conversion -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
#include "../SPIRVCommon/Pattern.h"
#include "mlir/Dialect/Index/IR/IndexDialect.h"
#include "mlir/Dialect/Index/IR/IndexOps.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
using namespace index;
namespace {
//===----------------------------------------------------------------------===//
// Trivial Conversions
//===----------------------------------------------------------------------===//
using ConvertIndexAdd = spirv::ElementwiseOpPattern<AddOp, spirv::IAddOp>;
using ConvertIndexSub = spirv::ElementwiseOpPattern<SubOp, spirv::ISubOp>;
using ConvertIndexMul = spirv::ElementwiseOpPattern<MulOp, spirv::IMulOp>;
using ConvertIndexDivS = spirv::ElementwiseOpPattern<DivSOp, spirv::SDivOp>;
using ConvertIndexDivU = spirv::ElementwiseOpPattern<DivUOp, spirv::UDivOp>;
using ConvertIndexRemS = spirv::ElementwiseOpPattern<RemSOp, spirv::SRemOp>;
using ConvertIndexRemU = spirv::ElementwiseOpPattern<RemUOp, spirv::UModOp>;
using ConvertIndexMaxS = spirv::ElementwiseOpPattern<MaxSOp, spirv::GLSMaxOp>;
using ConvertIndexMaxU = spirv::ElementwiseOpPattern<MaxUOp, spirv::GLUMaxOp>;
using ConvertIndexMinS = spirv::ElementwiseOpPattern<MinSOp, spirv::GLSMinOp>;
using ConvertIndexMinU = spirv::ElementwiseOpPattern<MinUOp, spirv::GLUMinOp>;
using ConvertIndexShl =
spirv::ElementwiseOpPattern<ShlOp, spirv::ShiftLeftLogicalOp>;
using ConvertIndexShrS =
spirv::ElementwiseOpPattern<ShrSOp, spirv::ShiftRightArithmeticOp>;
using ConvertIndexShrU =
spirv::ElementwiseOpPattern<ShrUOp, spirv::ShiftRightLogicalOp>;
/// It is the case that when we convert bitwise operations to SPIR-V operations
/// we must take into account the special pattern in SPIR-V that if the
/// operands are boolean values, then SPIR-V uses `SPIRVLogicalOp`. Otherwise,
/// for non-boolean operands, SPIR-V should use `SPIRVBitwiseOp`. However,
/// index.add is never a boolean operation so we can directly convert it to the
/// Bitwise[And|Or]Op.
using ConvertIndexAnd = spirv::ElementwiseOpPattern<AndOp, spirv::BitwiseAndOp>;
using ConvertIndexOr = spirv::ElementwiseOpPattern<OrOp, spirv::BitwiseOrOp>;
using ConvertIndexXor = spirv::ElementwiseOpPattern<XOrOp, spirv::BitwiseXorOp>;
//===----------------------------------------------------------------------===//
// ConvertConstantBool
//===----------------------------------------------------------------------===//
// Converts index.bool.constant operation to spirv.Constant.
struct ConvertIndexConstantBoolOpPattern final
: OpConversionPattern<BoolConstantOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(BoolConstantOp op, BoolConstantOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<spirv::ConstantOp>(op, op.getType(),
op.getValueAttr());
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertConstant
//===----------------------------------------------------------------------===//
// Converts index.constant op to spirv.Constant. Will truncate from i64 to i32
// when required.
struct ConvertIndexConstantOpPattern final : OpConversionPattern<ConstantOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(ConstantOp op, ConstantOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto *typeConverter = this->template getTypeConverter<SPIRVTypeConverter>();
Type indexType = typeConverter->getIndexType();
APInt value = op.getValue().trunc(typeConverter->getIndexTypeBitwidth());
rewriter.replaceOpWithNewOp<spirv::ConstantOp>(
op, indexType, IntegerAttr::get(indexType, value));
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexCeilDivS
//===----------------------------------------------------------------------===//
/// Convert `ceildivs(n, m)` into `x = m > 0 ? -1 : 1` and then
/// `n*m > 0 ? (n+x)/m + 1 : -(-n/m)`. Formula taken from the equivalent
/// conversion in IndexToLLVM.
struct ConvertIndexCeilDivSPattern final : OpConversionPattern<CeilDivSOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(CeilDivSOp op, CeilDivSOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Type n_type = n.getType();
Value m = adaptor.getRhs();
// Define the constants
Value zero = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, 0));
Value posOne = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, 1));
Value negOne = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, -1));
// Compute `x`.
Value mPos = rewriter.create<spirv::SGreaterThanOp>(loc, m, zero);
Value x = rewriter.create<spirv::SelectOp>(loc, mPos, negOne, posOne);
// Compute the positive result.
Value nPlusX = rewriter.create<spirv::IAddOp>(loc, n, x);
Value nPlusXDivM = rewriter.create<spirv::SDivOp>(loc, nPlusX, m);
Value posRes = rewriter.create<spirv::IAddOp>(loc, nPlusXDivM, posOne);
// Compute the negative result.
Value negN = rewriter.create<spirv::ISubOp>(loc, zero, n);
Value negNDivM = rewriter.create<spirv::SDivOp>(loc, negN, m);
Value negRes = rewriter.create<spirv::ISubOp>(loc, zero, negNDivM);
// Pick the positive result if `n` and `m` have the same sign and `n` is
// non-zero, i.e. `(n > 0) == (m > 0) && n != 0`.
Value nPos = rewriter.create<spirv::SGreaterThanOp>(loc, n, zero);
Value sameSign = rewriter.create<spirv::LogicalEqualOp>(loc, nPos, mPos);
Value nNonZero = rewriter.create<spirv::INotEqualOp>(loc, n, zero);
Value cmp = rewriter.create<spirv::LogicalAndOp>(loc, sameSign, nNonZero);
rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, cmp, posRes, negRes);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexCeilDivU
//===----------------------------------------------------------------------===//
/// Convert `ceildivu(n, m)` into `n == 0 ? 0 : (n-1)/m + 1`. Formula taken
/// from the equivalent conversion in IndexToLLVM.
struct ConvertIndexCeilDivUPattern final : OpConversionPattern<CeilDivUOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(CeilDivUOp op, CeilDivUOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Type n_type = n.getType();
Value m = adaptor.getRhs();
// Define the constants
Value zero = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, 0));
Value one = rewriter.create<spirv::ConstantOp>(loc, n_type,
IntegerAttr::get(n_type, 1));
// Compute the non-zero result.
Value minusOne = rewriter.create<spirv::ISubOp>(loc, n, one);
Value quotient = rewriter.create<spirv::UDivOp>(loc, minusOne, m);
Value plusOne = rewriter.create<spirv::IAddOp>(loc, quotient, one);
// Pick the result
Value cmp = rewriter.create<spirv::IEqualOp>(loc, n, zero);
rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, cmp, zero, plusOne);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexFloorDivS
//===----------------------------------------------------------------------===//
/// Convert `floordivs(n, m)` into `x = m < 0 ? 1 : -1` and then
/// `n*m < 0 ? -1 - (x-n)/m : n/m`. Formula taken from the equivalent conversion
/// in IndexToLLVM.
struct ConvertIndexFloorDivSPattern final : OpConversionPattern<FloorDivSOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(FloorDivSOp op, FloorDivSOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Type n_type = n.getType();
Value m = adaptor.getRhs();
// Define the constants
Value zero = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, 0));
Value posOne = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, 1));
Value negOne = rewriter.create<spirv::ConstantOp>(
loc, n_type, IntegerAttr::get(n_type, -1));
// Compute `x`.
Value mNeg = rewriter.create<spirv::SLessThanOp>(loc, m, zero);
Value x = rewriter.create<spirv::SelectOp>(loc, mNeg, posOne, negOne);
// Compute the negative result
Value xMinusN = rewriter.create<spirv::ISubOp>(loc, x, n);
Value xMinusNDivM = rewriter.create<spirv::SDivOp>(loc, xMinusN, m);
Value negRes = rewriter.create<spirv::ISubOp>(loc, negOne, xMinusNDivM);
// Compute the positive result.
Value posRes = rewriter.create<spirv::SDivOp>(loc, n, m);
// Pick the negative result if `n` and `m` have different signs and `n` is
// non-zero, i.e. `(n < 0) != (m < 0) && n != 0`.
Value nNeg = rewriter.create<spirv::SLessThanOp>(loc, n, zero);
Value diffSign = rewriter.create<spirv::LogicalNotEqualOp>(loc, nNeg, mNeg);
Value nNonZero = rewriter.create<spirv::INotEqualOp>(loc, n, zero);
Value cmp = rewriter.create<spirv::LogicalAndOp>(loc, diffSign, nNonZero);
rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, cmp, posRes, negRes);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexCast
//===----------------------------------------------------------------------===//
/// Convert a cast op. If the materialized index type is the same as the other
/// type, fold away the op. Otherwise, use the Convert SPIR-V operation.
/// Signed casts sign extend when the result bitwidth is larger. Unsigned casts
/// zero extend when the result bitwidth is larger.
template <typename CastOp, typename ConvertOp>
struct ConvertIndexCast final : OpConversionPattern<CastOp> {
using OpConversionPattern<CastOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(CastOp op, typename CastOp::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto *typeConverter = this->template getTypeConverter<SPIRVTypeConverter>();
Type indexType = typeConverter->getIndexType();
Type srcType = adaptor.getInput().getType();
Type dstType = op.getType();
if (isa<IndexType>(srcType)) {
srcType = indexType;
}
if (isa<IndexType>(dstType)) {
dstType = indexType;
}
if (srcType == dstType) {
rewriter.replaceOp(op, adaptor.getInput());
} else {
rewriter.template replaceOpWithNewOp<ConvertOp>(op, dstType,
adaptor.getOperands());
}
return success();
}
};
using ConvertIndexCastS = ConvertIndexCast<CastSOp, spirv::SConvertOp>;
using ConvertIndexCastU = ConvertIndexCast<CastUOp, spirv::UConvertOp>;
//===----------------------------------------------------------------------===//
// ConvertIndexCmp
//===----------------------------------------------------------------------===//
// Helper template to replace the operation
template <typename ICmpOp>
static LogicalResult rewriteCmpOp(CmpOp op, CmpOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) {
rewriter.replaceOpWithNewOp<ICmpOp>(op, adaptor.getLhs(), adaptor.getRhs());
return success();
}
struct ConvertIndexCmpPattern final : OpConversionPattern<CmpOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(CmpOp op, CmpOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// We must convert the predicates to the corresponding int comparions.
switch (op.getPred()) {
case IndexCmpPredicate::EQ:
return rewriteCmpOp<spirv::IEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::NE:
return rewriteCmpOp<spirv::INotEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::SGE:
return rewriteCmpOp<spirv::SGreaterThanEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::SGT:
return rewriteCmpOp<spirv::SGreaterThanOp>(op, adaptor, rewriter);
case IndexCmpPredicate::SLE:
return rewriteCmpOp<spirv::SLessThanEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::SLT:
return rewriteCmpOp<spirv::SLessThanOp>(op, adaptor, rewriter);
case IndexCmpPredicate::UGE:
return rewriteCmpOp<spirv::UGreaterThanEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::UGT:
return rewriteCmpOp<spirv::UGreaterThanOp>(op, adaptor, rewriter);
case IndexCmpPredicate::ULE:
return rewriteCmpOp<spirv::ULessThanEqualOp>(op, adaptor, rewriter);
case IndexCmpPredicate::ULT:
return rewriteCmpOp<spirv::ULessThanOp>(op, adaptor, rewriter);
}
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexSizeOf
//===----------------------------------------------------------------------===//
/// Lower `index.sizeof` to a constant with the value of the index bitwidth.
struct ConvertIndexSizeOf final : OpConversionPattern<SizeOfOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(SizeOfOp op, SizeOfOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto *typeConverter = this->template getTypeConverter<SPIRVTypeConverter>();
Type indexType = typeConverter->getIndexType();
unsigned bitwidth = typeConverter->getIndexTypeBitwidth();
rewriter.replaceOpWithNewOp<spirv::ConstantOp>(
op, indexType, IntegerAttr::get(indexType, bitwidth));
return success();
}
};
} // namespace
//===----------------------------------------------------------------------===//
// Pattern Population
//===----------------------------------------------------------------------===//
void index::populateIndexToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
RewritePatternSet &patterns) {
patterns.add<
// clang-format off
ConvertIndexAdd,
ConvertIndexSub,
ConvertIndexMul,
ConvertIndexDivS,
ConvertIndexDivU,
ConvertIndexRemS,
ConvertIndexRemU,
ConvertIndexMaxS,
ConvertIndexMaxU,
ConvertIndexMinS,
ConvertIndexMinU,
ConvertIndexShl,
ConvertIndexShrS,
ConvertIndexShrU,
ConvertIndexAnd,
ConvertIndexOr,
ConvertIndexXor,
ConvertIndexConstantBoolOpPattern,
ConvertIndexConstantOpPattern,
ConvertIndexCeilDivSPattern,
ConvertIndexCeilDivUPattern,
ConvertIndexFloorDivSPattern,
ConvertIndexCastS,
ConvertIndexCastU,
ConvertIndexCmpPattern,
ConvertIndexSizeOf
>(typeConverter, patterns.getContext());
}
//===----------------------------------------------------------------------===//
// ODS-Generated Definitions
//===----------------------------------------------------------------------===//
namespace mlir {
#define GEN_PASS_DEF_CONVERTINDEXTOSPIRVPASS
#include "mlir/Conversion/Passes.h.inc"
} // namespace mlir
//===----------------------------------------------------------------------===//
// Pass Definition
//===----------------------------------------------------------------------===//
namespace {
struct ConvertIndexToSPIRVPass
: public impl::ConvertIndexToSPIRVPassBase<ConvertIndexToSPIRVPass> {
using Base::Base;
void runOnOperation() override {
Operation *op = getOperation();
spirv::TargetEnvAttr targetAttr = spirv::lookupTargetEnvOrDefault(op);
std::unique_ptr<SPIRVConversionTarget> target =
SPIRVConversionTarget::get(targetAttr);
SPIRVConversionOptions options;
options.use64bitIndex = this->use64bitIndex;
SPIRVTypeConverter typeConverter(targetAttr, options);
// Use UnrealizedConversionCast as the bridge so that we don't need to pull
// in patterns for other dialects.
target->addLegalOp<UnrealizedConversionCastOp>();
// Allow the spirv operations we are converting to
target->addLegalDialect<spirv::SPIRVDialect>();
// Fail hard when there are any remaining 'index' ops.
target->addIllegalDialect<index::IndexDialect>();
RewritePatternSet patterns(&getContext());
index::populateIndexToSPIRVPatterns(typeConverter, patterns);
if (failed(applyPartialConversion(op, *target, std::move(patterns))))
signalPassFailure();
}
};
} // namespace
|