Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Interfaces/InferIntRangeInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "llvm/IR/IntrinsicsNVPTX.h"

Expand Down
20 changes: 18 additions & 2 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Dialect/LLVMIR/BasicPtxBuilderInterface.td"
include "mlir/Interfaces/InferIntRangeInterface.td"

def LLVM_PointerGeneric : LLVM_PointerInAddressSpace<0>;
def LLVM_PointerGlobal : LLVM_PointerInAddressSpace<1>;
Expand Down Expand Up @@ -134,8 +135,8 @@ class NVVM_SpecialRegisterOp<string mnemonic, list<Trait> traits = []> :
let assemblyFormat = "attr-dict `:` type($res)";
}

class NVVM_SpecialRangeableRegisterOp<string mnemonic, list<Trait> traits = []> :
NVVM_SpecialRegisterOp<mnemonic, traits> {
class NVVM_SpecialRangeableRegisterOp<string mnemonic> :
NVVM_SpecialRegisterOp<mnemonic, [DeclareOpInterfaceMethods<InferIntRangeInterface, ["inferResultRanges"]>]> {
let arguments = (ins OptionalAttr<LLVM_ConstantRangeAttr>:$range);
let assemblyFormat = "(`range` $range^)? attr-dict `:` type($res)";
let llvmBuilder = baseLlvmBuilder # setRangeRetAttrCode # baseLlvmBuilderCoda;
Expand All @@ -147,6 +148,21 @@ class NVVM_SpecialRangeableRegisterOp<string mnemonic, list<Trait> traits = []>
build($_builder, $_state, resultType, ::mlir::LLVM::ConstantRangeAttr{});
}]>
];

// Define this method for the InferIntRangeInterface.
let extraClassDefinition = [{
// Infer the result ranges based on the range attribute.
void $cppClass::inferResultRanges(
ArrayRef<::mlir::ConstantIntRanges> argRanges,
SetIntRangeFn setResultRanges) {
if (auto rangeAttr = getOperation()->getAttrOfType<LLVM::ConstantRangeAttr>("range")) {
setResultRanges(getResult(),
{rangeAttr.getLower(), rangeAttr.getUpper(),
rangeAttr.getLower(), rangeAttr.getUpper()});
}
}
}];

}

//===----------------------------------------------------------------------===//
Expand Down
28 changes: 28 additions & 0 deletions mlir/test/Dialect/LLVMIR/nvvm-test-range.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: mlir-opt -int-range-optimizations -canonicalize %s | FileCheck %s
gpu.module @module{
gpu.func @kernel_1() kernel {
%tidx = nvvm.read.ptx.sreg.tid.x range <i32, 0, 32> : i32
%tidy = nvvm.read.ptx.sreg.tid.y range <i32, 0, 128> : i32
%tidz = nvvm.read.ptx.sreg.tid.z range <i32, 0, 4> : i32
%c64 = arith.constant 64 : i32

%1 = arith.cmpi sgt, %tidx, %c64 : i32
scf.if %1 {
gpu.printf "threadidx"
}
%2 = arith.cmpi sgt, %tidy, %c64 : i32
scf.if %2 {
gpu.printf "threadidy"
}
%3 = arith.cmpi sgt, %tidz, %c64 : i32
scf.if %3 {
gpu.printf "threadidz"
}
gpu.return
}
}

// CHECK-LABEL: gpu.func @kernel_1
// CHECK-NOT: gpu.printf "threadidx"
// CHECK: gpu.printf "threadidy"
// CHECK-NOT: gpu.printf "threadidz"
Loading