MLIR 24.0.0git
NVVMIndexSRegOps.cpp
Go to the documentation of this file.
1//===- NVVMIndexSRegOps.cpp - NVVM dialect index registers ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ops of op partition `IndexSReg` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/NVVMOps.h"
16#include "mlir/IR/Diagnostics.h"
17#include "mlir/IR/Operation.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/IR/ConstantRange.h"
20
21#include <optional>
22
23using namespace mlir;
24using namespace mlir::NVVM;
25
26/// Infer the result ranges for the NVVM SpecialRangeableRegisterOp that might
27/// have ConstantRangeAttr.
28static void nvvmInferResultRanges(std::optional<LLVM::ConstantRangeAttr> range,
31 SetIntRangeFn setResultRanges) {
32 if (range) {
33 // ConstantRange uses a half-open interval of bit patterns, whereas
34 // ConstantIntRanges uses inclusive signed and unsigned bounds. Derive
35 // each interpretation separately to handle wrapping and sign boundaries.
36 llvm::ConstantRange valueRange(range->getLower(), range->getUpper());
37 if (!valueRange.isEmptySet()) {
38 setResultRanges(result,
39 {valueRange.getUnsignedMin(), valueRange.getUnsignedMax(),
40 valueRange.getSignedMin(), valueRange.getSignedMax()});
41 return;
42 }
43 }
44 // ConstantIntRanges cannot represent an empty set. Conservatively fall back
45 // to the full range rather than deriving invalid inclusive bounds.
46 setResultRanges(result, IntegerValueRange::getMaxRange(result).getValue());
47}
48
49/// Verify the range attribute satisfies LLVM ConstantRange constructor
50/// requirements for NVVM SpecialRangeableRegisterOp.
51static LogicalResult
53 std::optional<LLVM::ConstantRangeAttr> rangeAttr) {
54 if (!rangeAttr)
55 return success();
56
57 const llvm::APInt &lower = rangeAttr->getLower();
58 const llvm::APInt &upper = rangeAttr->getUpper();
59
60 // Check LLVM ConstantRange constructor condition
61 if (lower == upper && !lower.isMaxValue() && !lower.isMinValue()) {
62 unsigned bitWidth = lower.getBitWidth();
63 llvm::APInt minVal = llvm::APInt::getMinValue(bitWidth);
64 llvm::APInt maxVal = llvm::APInt::getMaxValue(bitWidth);
65 return op->emitOpError(
66 "invalid range attribute: Lower == Upper, but they aren't min (")
67 << llvm::toString(minVal, 10, false) << ") or max ("
68 << llvm::toString(maxVal, 10, false)
69 << ") value! This is an invalid constant range.";
70 }
71
72 return success();
73}
74
75#define GET_OP_CLASSES
76#include "mlir/Dialect/LLVMIR/NVVMIndexSRegOps.cpp.inc"
return success()
static void nvvmInferResultRanges(std::optional< LLVM::ConstantRangeAttr > range, Value result, ArrayRef<::mlir::ConstantIntRanges > argRanges, SetIntRangeFn setResultRanges)
Infer the result ranges for the NVVM SpecialRangeableRegisterOp that might have ConstantRangeAttr.
static LogicalResult verifyConstantRangeAttr(Operation *op, std::optional< LLVM::ConstantRangeAttr > rangeAttr)
Verify the range attribute satisfies LLVM ConstantRange constructor requirements for NVVM SpecialRang...
static IntegerValueRange getMaxRange(Value value)
Create a maximal range ([0, uint_max(t)] / [int_min(t), int_max(t)]) range that is used to mark the v...
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.
llvm::function_ref< void(Value, const ConstantIntRanges &)> SetIntRangeFn
The type of the setResultRanges callback provided to ops implementing InferIntRangeInterface.