[llvm] [NVPTX] designate fabs and fneg as free (PR #121513)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 15:13:47 PST 2025


================
@@ -261,6 +261,9 @@ class NVPTXTargetLowering : public TargetLowering {
     return true;
   }
 
+  bool isFAbsFree(EVT VT) const override { return true; }
+  bool isFNegFree(EVT VT) const override { return true; }
----------------
Artem-B wrote:

My general rule of thumb is that it's likely beneficial to use specific PTX instruction, if there's a matching h/w instruction, but let LLVM handle it if it turns into generic code that LLVM can do itself. In this case I'm not convinced that folding some cases into "FADD" with a negated argument counts, as LLVM would be able to do the folding into `fsub`, which, I'm pretty sure, would result in the same `FADD a, -b` on the SASS level.

This patch generates slightly more concise and readable PTX, with the downside of shifting some potential optimizations from LLVM to ptxas. Overall it looks like a wash to me.

There's also a question of whether the "free" applies to all types equally. E.g. for `bf16x2` the sign folding no longer happens: https://cuda.godbolt.org/z/e15cG6jdM


https://github.com/llvm/llvm-project/pull/121513


More information about the llvm-commits mailing list