Skip to content

Commit

Permalink
Allow 16 bit floating point operand for LLVM_AtomicRMWOp
Browse files Browse the repository at this point in the history
As far as AMDGPU target supports vectorization for atomic_rmw operation,
allow construction of LLVM_AtomicRMWOp with 16 bit floating point values.

See also: llvm#94845, llvm#95393, llvm#95394

Signed-off-by: Ilya Veselov <iveselov.nn@gmail.com>
  • Loading branch information
joviliast committed Sep 30, 2024
1 parent 587eaef commit 6cb4a43
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,8 @@ def LLVM_ConstantOp
// Atomic operations.
//

def LLVM_AtomicRMWType : AnyTypeOf<[LLVM_AnyFloat, LLVM_AnyPointer, AnySignlessInteger]>;
def LLVM_AtomicRMWType
: AnyTypeOf<[LLVM_AnyPointer, AnySignlessInteger, LLVM_ScalarOrVectorOf<LLVM_AnyFloat>]>;

def LLVM_AtomicRMWOp : LLVM_MemAccessOpBase<"atomicrmw", [
TypesMatchWith<"result #0 and operand #1 have the same type",
Expand Down
2 changes: 1 addition & 1 deletion mlir/include/mlir/IR/BuiltinTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ class VectorType::Builder {

Builder &setShape(ArrayRef<int64_t> newShape,
ArrayRef<bool> newIsScalableDim = {}) {
shape = newShape;
scalableDims = newIsScalableDim;
shape = newShape;
return *this;
}

Expand Down
16 changes: 13 additions & 3 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3008,9 +3008,19 @@ void AtomicRMWOp::build(OpBuilder &builder, OperationState &state,

LogicalResult AtomicRMWOp::verify() {
auto valType = getVal().getType();
if (getBinOp() == AtomicBinOp::fadd || getBinOp() == AtomicBinOp::fsub ||
getBinOp() == AtomicBinOp::fmin || getBinOp() == AtomicBinOp::fmax) {
if (!mlir::LLVM::isCompatibleFloatingPointType(valType))
if (getBinOp() == AtomicBinOp::fadd && isCompatibleVectorType(valType)) {
// Currently, only fadd operation supports fixed vector operands.
if (isScalableVectorType(valType))
return emitOpError("expected LLVM IR fixed vector type");
Type elemType = getVectorElementType(valType);
if (!(isCompatibleFloatingPointType(elemType) &&
elemType.getIntOrFloatBitWidth() == 16))
return emitOpError("unexpected LLVM IR type for vector element");
} else if (getBinOp() == AtomicBinOp::fadd ||
getBinOp() == AtomicBinOp::fsub ||
getBinOp() == AtomicBinOp::fmin ||
getBinOp() == AtomicBinOp::fmax) {
if (!isCompatibleFloatingPointType(valType))
return emitOpError("expected LLVM IR floating point type");
} else if (getBinOp() == AtomicBinOp::xchg) {
DataLayout dataLayout = DataLayout::closest(*this);
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ verifyVectorConstructionInvariants(function_ref<InFlightDiagnostic()> emitError,
if (numElements == 0)
return emitError() << "the number of vector elements must be positive";

if (!VecTy::isValidElementType(elementType))
if (!VecTy::isValidElementType(elementType) ^ VectorType::isValidElementType(elementType))
return emitError() << "invalid vector element type";

return success();
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/LLVMIR/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ func.func @atomicrmw_expected_float(%i32_ptr : !llvm.ptr, %i32 : i32) {

// -----

func.func @atomicrmw_unexpected_vector_element(%i32_ptr : !llvm.ptr, %i16_fvec : vector<[3]xi16>) {
// expected-error@+1 {{unexpected LLVM IR type for vector element}}
%0 = llvm.atomicrmw fadd %i32_ptr, %i16_fvec unordered : !llvm.ptr, i32
llvm.return
}

// -----

func.func @atomicrmw_unexpected_xchg_type(%i1_ptr : !llvm.ptr, %i1 : i1) {
// expected-error@+1 {{unexpected LLVM IR type for 'xchg' bin_op}}
%0 = llvm.atomicrmw xchg %i1_ptr, %i1 unordered : !llvm.ptr, i1
Expand Down

0 comments on commit 6cb4a43

Please sign in to comment.