Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV][TTI] Refactor getCastInstrCost to exit early #86619

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

arcbbb
Copy link
Contributor

@arcbbb arcbbb commented Mar 26, 2024

To reduce the indentation by using early returns, this patch hoist the return for illegal type and non vector type earlier.

It should mostly be an NFC.

To reduce the indentation by using early returns, this patch
hoist the return for illegal type and non vector type earlier.

It should mostly be an NFC.
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 26, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Shih-Po Hung (arcbbb)

Changes

To reduce the indentation by using early returns, this patch hoist the return for illegal type and non vector type earlier.

It should mostly be an NFC.


Full diff: https://github.com/llvm/llvm-project/pull/86619.diff

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp (+65-68)
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index f75b3d3caa62f2..65142a03f0a624 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -897,76 +897,73 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
                                                TTI::CastContextHint CCH,
                                                TTI::TargetCostKind CostKind,
                                                const Instruction *I) {
-  if (isa<VectorType>(Dst) && isa<VectorType>(Src)) {
-    // FIXME: Need to compute legalizing cost for illegal types.
-    if (!isTypeLegal(Src) || !isTypeLegal(Dst))
-      return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
-
-    // Skip if element size of Dst or Src is bigger than ELEN.
-    if (Src->getScalarSizeInBits() > ST->getELen() ||
-        Dst->getScalarSizeInBits() > ST->getELen())
-      return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
-
-    int ISD = TLI->InstructionOpcodeToISD(Opcode);
-    assert(ISD && "Invalid opcode");
-
-    // FIXME: Need to consider vsetvli and lmul.
-    int PowDiff = (int)Log2_32(Dst->getScalarSizeInBits()) -
-                  (int)Log2_32(Src->getScalarSizeInBits());
-    switch (ISD) {
-    case ISD::SIGN_EXTEND:
-    case ISD::ZERO_EXTEND:
-      if (Src->getScalarSizeInBits() == 1) {
-        // We do not use vsext/vzext to extend from mask vector.
-        // Instead we use the following instructions to extend from mask vector:
-        // vmv.v.i v8, 0
-        // vmerge.vim v8, v8, -1, v0
-        return 2;
-      }
-      return 1;
-    case ISD::TRUNCATE:
-      if (Dst->getScalarSizeInBits() == 1) {
-        // We do not use several vncvt to truncate to mask vector. So we could
-        // not use PowDiff to calculate it.
-        // Instead we use the following instructions to truncate to mask vector:
-        // vand.vi v8, v8, 1
-        // vmsne.vi v0, v8, 0
-        return 2;
-      }
-      [[fallthrough]];
-    case ISD::FP_EXTEND:
-    case ISD::FP_ROUND:
-      // Counts of narrow/widen instructions.
-      return std::abs(PowDiff);
-    case ISD::FP_TO_SINT:
-    case ISD::FP_TO_UINT:
-    case ISD::SINT_TO_FP:
-    case ISD::UINT_TO_FP:
-      if (Src->getScalarSizeInBits() == 1 || Dst->getScalarSizeInBits() == 1) {
-        // The cost of convert from or to mask vector is different from other
-        // cases. We could not use PowDiff to calculate it.
-        // For mask vector to fp, we should use the following instructions:
-        // vmv.v.i v8, 0
-        // vmerge.vim v8, v8, -1, v0
-        // vfcvt.f.x.v v8, v8
-
-        // And for fp vector to mask, we use:
-        // vfncvt.rtz.x.f.w v9, v8
-        // vand.vi v8, v9, 1
-        // vmsne.vi v0, v8, 0
-        return 3;
-      }
-      if (std::abs(PowDiff) <= 1)
-        return 1;
-      // Backend could lower (v[sz]ext i8 to double) to vfcvt(v[sz]ext.f8 i8),
-      // so it only need two conversion.
-      if (Src->isIntOrIntVectorTy())
-        return 2;
-      // Counts of narrow/widen instructions.
-      return std::abs(PowDiff);
+  bool IsVectorType = isa<VectorType>(Dst) && isa<VectorType>(Src);
+  bool IsTypeLegal = isTypeLegal(Src) && isTypeLegal(Dst) &&
+                     (Src->getScalarSizeInBits() <= ST->getELen()) &&
+                     (Dst->getScalarSizeInBits() <= ST->getELen());
+
+  // FIXME: Need to compute legalizing cost for illegal types.
+  if (!IsVectorType || !IsTypeLegal)
+    return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
+
+  int ISD = TLI->InstructionOpcodeToISD(Opcode);
+  assert(ISD && "Invalid opcode");
+
+  // FIXME: Need to consider vsetvli and lmul.
+  int PowDiff = (int)Log2_32(Dst->getScalarSizeInBits()) -
+                (int)Log2_32(Src->getScalarSizeInBits());
+  switch (ISD) {
+  case ISD::SIGN_EXTEND:
+  case ISD::ZERO_EXTEND:
+    if (Src->getScalarSizeInBits() == 1) {
+      // We do not use vsext/vzext to extend from mask vector.
+      // Instead we use the following instructions to extend from mask vector:
+      // vmv.v.i v8, 0
+      // vmerge.vim v8, v8, -1, v0
+      return 2;
     }
+    return 1;
+  case ISD::TRUNCATE:
+    if (Dst->getScalarSizeInBits() == 1) {
+      // We do not use several vncvt to truncate to mask vector. So we could
+      // not use PowDiff to calculate it.
+      // Instead we use the following instructions to truncate to mask vector:
+      // vand.vi v8, v8, 1
+      // vmsne.vi v0, v8, 0
+      return 2;
+    }
+    [[fallthrough]];
+  case ISD::FP_EXTEND:
+  case ISD::FP_ROUND:
+    // Counts of narrow/widen instructions.
+    return std::abs(PowDiff);
+  case ISD::FP_TO_SINT:
+  case ISD::FP_TO_UINT:
+  case ISD::SINT_TO_FP:
+  case ISD::UINT_TO_FP:
+    if (Src->getScalarSizeInBits() == 1 || Dst->getScalarSizeInBits() == 1) {
+      // The cost of convert from or to mask vector is different from other
+      // cases. We could not use PowDiff to calculate it.
+      // For mask vector to fp, we should use the following instructions:
+      // vmv.v.i v8, 0
+      // vmerge.vim v8, v8, -1, v0
+      // vfcvt.f.x.v v8, v8
+
+      // And for fp vector to mask, we use:
+      // vfncvt.rtz.x.f.w v9, v8
+      // vand.vi v8, v9, 1
+      // vmsne.vi v0, v8, 0
+      return 3;
+    }
+    if (std::abs(PowDiff) <= 1)
+      return 1;
+    // Backend could lower (v[sz]ext i8 to double) to vfcvt(v[sz]ext.f8 i8),
+    // so it only need two conversion.
+    if (Src->isIntOrIntVectorTy())
+      return 2;
+    // Counts of narrow/widen instructions.
+    return std::abs(PowDiff);
   }
-  return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
 }
 
 unsigned RISCVTTIImpl::getEstimatedVLFor(VectorType *Ty) {

Copy link
Contributor

@lukel97 lukel97 left a comment

Choose a reason for hiding this comment

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

LGTM

@arcbbb arcbbb merged commit 817f453 into llvm:main Mar 26, 2024
5 of 6 checks passed
@arcbbb arcbbb deleted the early-exit-tti-cast branch March 26, 2024 06:15
}
return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You're missing a return if none of the cases in the switch match

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp:967:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
  967 | }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the trouble. I fix it with commit 5dc0c75

@zeroomega
Copy link
Contributor

Hi,

This patch is causing an assertion error when building builtins-riscv64-unknown-linux-gnu:

FAILED: CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o 
/b/s/w/ir/x/w/llvm_build/./bin/clang --target=riscv64-unknown-linux-gnu --sysroot=/b/s/w/ir/x/w/cipd/focal -DVISIBILITY_HIDDEN  --target=riscv64-unknown-linux-gnu -O2 -g -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -MD -MT CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -MF CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o.d -o CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -c /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/builtins/comparedf2.c
clang: llvm/lib/Target/RISCV/RISCVSubtarget.h:184: unsigned int llvm::RISCVSubtarget::getELen() const: Assertion `hasVInstructions() && "Expected V extension"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /b/s/w/ir/x/w/llvm_build/./bin/clang --target=riscv64-unknown-linux-gnu --sysroot=/b/s/w/ir/x/w/cipd/focal -DVISIBILITY_HIDDEN --target=riscv64-unknown-linux-gnu -O2 -g -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -MD -MT CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -MF CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o.d -o CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -c /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/builtins/comparedf2.c
1.	<eof> parser at end of file
2.	Optimizer
#0 0x0000558c74b4ae08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/b/s/w/ir/x/w/llvm_build/./bin/clang+0x8948e08)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Fuchsia clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 817f453aa576286aaca0a6b0244e6ab08516b80c)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /b/s/w/ir/x/w/llvm_build/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /b/s/w/ir/x/w/llvm_build/clang-crashreports/comparedf2-7e2acc.c
clang: note: diagnostic msg: /b/s/w/ir/x/w/llvm_build/clang-crashreports/comparedf2-7e2acc.sh
clang: note: diagnostic msg: 

********************

This issue still persist after your fix forward patch landed. Could you revert your changes and fix them and reland them?
Failed build: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8752425016802502289/overview

Entire stdout from the build: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8752425016802502289/+/u/clang/build/stdout

Thanks.

@topperc
Copy link
Collaborator

topperc commented Mar 26, 2024

Hi,

This patch is causing an assertion error when building builtins-riscv64-unknown-linux-gnu:

FAILED: CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o 
/b/s/w/ir/x/w/llvm_build/./bin/clang --target=riscv64-unknown-linux-gnu --sysroot=/b/s/w/ir/x/w/cipd/focal -DVISIBILITY_HIDDEN  --target=riscv64-unknown-linux-gnu -O2 -g -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -MD -MT CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -MF CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o.d -o CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -c /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/builtins/comparedf2.c
clang: llvm/lib/Target/RISCV/RISCVSubtarget.h:184: unsigned int llvm::RISCVSubtarget::getELen() const: Assertion `hasVInstructions() && "Expected V extension"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /b/s/w/ir/x/w/llvm_build/./bin/clang --target=riscv64-unknown-linux-gnu --sysroot=/b/s/w/ir/x/w/cipd/focal -DVISIBILITY_HIDDEN --target=riscv64-unknown-linux-gnu -O2 -g -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -MD -MT CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -MF CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o.d -o CMakeFiles/clang_rt.builtins-riscv64.dir/comparedf2.c.o -c /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/builtins/comparedf2.c
1.	<eof> parser at end of file
2.	Optimizer
#0 0x0000558c74b4ae08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/b/s/w/ir/x/w/llvm_build/./bin/clang+0x8948e08)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Fuchsia clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 817f453aa576286aaca0a6b0244e6ab08516b80c)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /b/s/w/ir/x/w/llvm_build/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /b/s/w/ir/x/w/llvm_build/clang-crashreports/comparedf2-7e2acc.c
clang: note: diagnostic msg: /b/s/w/ir/x/w/llvm_build/clang-crashreports/comparedf2-7e2acc.sh
clang: note: diagnostic msg: 

********************

This issue still persist after your fix forward patch landed. Could you revert your changes and fix them and reland them? Failed build: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8752425016802502289/overview

Entire stdout from the build: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8752425016802502289/+/u/clang/build/stdout

Thanks.

Shbould be fixed after 2fbc40d

@arcbbb
Copy link
Contributor Author

arcbbb commented Mar 27, 2024

Thanks @topperc and @zeroomega !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants