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 d2dec08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 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_ScalarOrFixedVectorOf<LLVM_AnyFloat>]>;

def LLVM_AtomicRMWOp : LLVM_MemAccessOpBase<"atomicrmw", [
TypesMatchWith<"result #0 and operand #1 have the same type",
Expand Down
17 changes: 14 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,20 @@ 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 (llvm::isa<LLVMScalableVectorType>(valType))
return emitOpError("unexpected LLVM IR vector type");

if (getBinOp() == AtomicBinOp::fadd && llvm::isa<LLVMFixedVectorType>(valType)) {
// Currently, only fadd operation supports fixed vector operands.
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 @@ -684,7 +684,7 @@ LLVMFixedVectorType::getChecked(function_ref<InFlightDiagnostic()> emitError,
}

bool LLVMFixedVectorType::isValidElementType(Type type) {
return llvm::isa<LLVMPointerType, LLVMPPCFP128Type>(type);
return llvm::isa<LLVMPointerType, LLVMPPCFP128Type, Float16Type, BFloat16Type>(type);
}

LogicalResult
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 d2dec08

Please sign in to comment.