Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions llvm/docs/NVPTXUsage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,58 @@ space casted to this space), 1 is returned, otherwise 0 is returned.
Arithmetic Intrinsics
---------------------

'``llvm.nvvm.fabs.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

.. code-block:: llvm

declare float @llvm.nvvm.fabs.f32(float %a)
declare double @llvm.nvvm.fabs.f64(double %a)
declare half @llvm.nvvm.fabs.f16(half %a)
declare <2 x bfloat> @llvm.nvvm.fabs.v2bf16(<2 x bfloat> %a)

Overview:
"""""""""

The '``llvm.nvvm.fabs.*``' intrinsics return the absolute value of the operand.

Semantics:
""""""""""

Unlike, '``llvm.fabs.*``', these intrinsics do not perfectly preserve NaN
values. Instead, a NaN input yeilds an unspecified NaN output. The exception to
this rule is the double precision variant, for which NaN is preserved.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "exception" here seems like it's letting too much of the implementation leak through... it's hard to understand the semantics, and it'll make it very hard to transition if we decide to turn this into a target-independent intrinsic. Maybe just say it's always unspecified, and juts use the regular llvm.fabs.f64 where the nan handling matters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, this is a good point. I've updated the docs to remove the exception. A NaN will now always yield an unspecified NaN output.



'``llvm.nvvm.fabs.ftz.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

.. code-block:: llvm

declare float @llvm.nvvm.fabs.ftz.f32(float %a)
declare half @llvm.nvvm.fabs.ftz.f16(half %a)
declare <2 x half> @llvm.nvvm.fabs.ftz.v2f16(<2 x half> %a)

Overview:
"""""""""

The '``llvm.nvvm.fabs.ftz.*``' intrinsics return the absolute value of the
operand, flushing subnormals to sign preserving zero.

Semantics:
""""""""""

Before the absolute value is taken, the input is flushed to sign preserving
zero if it is a subnormal. In addtion, unlike '``llvm.fabs.*``', a NaN input
yields an unspecified NaN output.


'``llvm.nvvm.idp2a.[us].[us]``' Intrinsics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
14 changes: 7 additions & 7 deletions llvm/include/llvm/IR/IntrinsicsNVVM.td
Original file line number Diff line number Diff line change
Expand Up @@ -1039,18 +1039,18 @@ let TargetPrefix = "nvvm" in {
// Abs
//

def int_nvvm_fabs_ftz_f : ClangBuiltin<"__nvvm_fabs_ftz_f">,
DefaultAttrsIntrinsic<[llvm_float_ty], [llvm_float_ty], [IntrNoMem, IntrSpeculatable]>;
def int_nvvm_fabs_f : ClangBuiltin<"__nvvm_fabs_f">,
DefaultAttrsIntrinsic<[llvm_float_ty], [llvm_float_ty], [IntrNoMem, IntrSpeculatable]>;
def int_nvvm_fabs_d : ClangBuiltin<"__nvvm_fabs_d">,
DefaultAttrsIntrinsic<[llvm_double_ty], [llvm_double_ty], [IntrNoMem, IntrSpeculatable]>;
def int_nvvm_fabs_ftz :
DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;

def int_nvvm_fabs :
DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;
//
// Abs, Neg bf16, bf16x2
//

foreach unary = ["abs", "neg"] in {
foreach unary = ["neg"] in {
def int_nvvm_ # unary # _bf16 :
ClangBuiltin<!strconcat("__nvvm_", unary, "_bf16")>,
DefaultAttrsIntrinsic<[llvm_bfloat_ty], [llvm_bfloat_ty], [IntrNoMem]>;
Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/IR/AutoUpgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,6 @@ static bool upgradeArmOrAarch64IntrinsicFunction(bool IsArm, Function *F,
}

static Intrinsic::ID shouldUpgradeNVPTXBF16Intrinsic(StringRef Name) {
if (Name.consume_front("abs."))
return StringSwitch<Intrinsic::ID>(Name)
.Case("bf16", Intrinsic::nvvm_abs_bf16)
.Case("bf16x2", Intrinsic::nvvm_abs_bf16x2)
.Default(Intrinsic::not_intrinsic);

if (Name.consume_front("fma.rn."))
return StringSwitch<Intrinsic::ID>(Name)
.Case("bf16", Intrinsic::nvvm_fma_rn_bf16)
Expand Down Expand Up @@ -1291,7 +1285,8 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
bool Expand = false;
if (Name.consume_front("abs."))
// nvvm.abs.{i,ii}
Expand = Name == "i" || Name == "ll";
Expand =
Name == "i" || Name == "ll" || Name == "bf16" || Name == "bf16x2";
else if (Name == "clz.ll" || Name == "popc.ll" || Name == "h2f" ||
Name == "swap.lo.hi.b64")
Expand = true;
Expand Down Expand Up @@ -2311,6 +2306,13 @@ static Value *upgradeNVVMIntrinsicCall(StringRef Name, CallBase *CI,
Value *Cmp = Builder.CreateICmpSGE(
Arg, llvm::Constant::getNullValue(Arg->getType()), "abs.cond");
Rep = Builder.CreateSelect(Cmp, Arg, Neg, "abs");
} else if (Name == "abs.bf16" || Name == "abs.bf16x2") {
Type *Ty = (Name == "abs.bf16")
? Builder.getBFloatTy()
: FixedVectorType::get(Builder.getBFloatTy(), 2);
Value *Arg = Builder.CreateBitCast(CI->getArgOperand(0), Ty);
Value *Abs = Builder.CreateUnaryIntrinsic(Intrinsic::nvvm_fabs, Arg);
Rep = Builder.CreateBitCast(Abs, CI->getType());
} else if (Name.starts_with("atomic.load.add.f32.p") ||
Name.starts_with("atomic.load.add.f64.p")) {
Value *Ptr = CI->getArgOperand(0);
Expand Down
19 changes: 11 additions & 8 deletions llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,17 @@ class RegTyInfo<ValueType ty, NVPTXRegClass rc, Operand imm, SDNode imm_node,
int Size = ty.Size;
}

def I16RT : RegTyInfo<i16, Int16Regs, i16imm, imm>;
def I32RT : RegTyInfo<i32, Int32Regs, i32imm, imm>;
def I64RT : RegTyInfo<i64, Int64Regs, i64imm, imm>;

def F32RT : RegTyInfo<f32, Float32Regs, f32imm, fpimm>;
def F64RT : RegTyInfo<f64, Float64Regs, f64imm, fpimm>;
def F16RT : RegTyInfo<f16, Int16Regs, f16imm, fpimm, supports_imm = 0>;
def BF16RT : RegTyInfo<bf16, Int16Regs, bf16imm, fpimm, supports_imm = 0>;
def I16RT : RegTyInfo<i16, Int16Regs, i16imm, imm>;
def I32RT : RegTyInfo<i32, Int32Regs, i32imm, imm>;
def I64RT : RegTyInfo<i64, Int64Regs, i64imm, imm>;

def F32RT : RegTyInfo<f32, Float32Regs, f32imm, fpimm>;
def F64RT : RegTyInfo<f64, Float64Regs, f64imm, fpimm>;
def F16RT : RegTyInfo<f16, Int16Regs, f16imm, fpimm, supports_imm = 0>;
def BF16RT : RegTyInfo<bf16, Int16Regs, bf16imm, fpimm, supports_imm = 0>;

def F16X2RT : RegTyInfo<v2f16, Int32Regs, ?, ?, supports_imm = 0>;
def BF16X2RT : RegTyInfo<v2bf16, Int32Regs, ?, ?, supports_imm = 0>;
Comment on lines +238 to +239
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I never thought of passing ? as an argument. That can indeed be convenient in some cases.



// Template for instructions which take three int64, int32, or int16 args.
Expand Down
Loading
Loading