This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fix int16 -> __fp16 conversion code gen
ClosedPublic

Authored by kito-cheng on Apr 27 2022, 2:29 AM.

Details

Summary

clang emit wrong code sequence for int16(short) to __fp16 conversion,
and that should fix the code gen directly is the right way I think,
but I found there is a FIXME comment in clang/Basic/TargetInfo.h say
that's should be removed in future so I think just let swich to using
generic LLVM IR rather than llvm.convert.to.fp16 intrinsics code gen
path is enough.

/// Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used
/// to convert to and from __fp16.
/// FIXME: This function should be removed once all targets stop using the
/// conversion intrinsics.
virtual bool useFP16ConversionIntrinsics() const {
  return true;
}

Diff Detail

Event Timeline

kito-cheng created this revision.Apr 27 2022, 2:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2022, 2:29 AM
kito-cheng requested review of this revision.Apr 27 2022, 2:29 AM
kito-cheng edited the summary of this revision. (Show Details)Apr 27 2022, 2:30 AM
kito-cheng added reviewers: asb, luismarques.
kito-cheng edited the summary of this revision. (Show Details)Apr 27 2022, 2:38 AM

It's seem like all targets need to return false in useFP16ConversionIntrinsics for correctness of int16 -> __fp16 conversion?

I'm not opposed to this patch, but here's some additional info

I think this is failing because we hit this code in CGExprScalar.cpp. Where SrcTy and DstTy are both i16 when using the conversion intrinsics.

// Ignore conversions like int -> uint.
if (SrcTy == DstTy) {
  llvm::dbgs() << "ctopper20\n";
  if (Opts.EmitImplicitIntegerSignChangeChecks)
    EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Src,
                               NoncanonicalDstType, Loc);

  return Src;
}

Something like this seems to fix it.

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index d3fe04d5a791..62662bf860c2 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1339,7 +1339,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
   }
 
   // Ignore conversions like int -> uint.
-  if (SrcTy == DstTy) {
+  if (SrcTy == DstTy && !DstType->isHalfType()) {
     if (Opts.EmitImplicitIntegerSignChangeChecks)
       EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Src,
                                  NoncanonicalDstType, Loc);
This revision is now accepted and ready to land.Apr 28 2022, 9:16 PM
This revision was landed with ongoing or failed builds.Apr 29 2022, 8:09 PM
This revision was automatically updated to reflect the committed changes.