[clang] [llvm] Adding splitdouble HLSL function (PR #109331)

Tex Riddell via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 23 17:52:43 PDT 2024


================
@@ -95,6 +99,133 @@ static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size,
   I->addAnnotationMetadata("auto-init");
 }
 
+static Value *handleHlslSplitdouble(const CallExpr *E, CodeGenFunction *CGF) {
+  Value *Op0 = CGF->EmitScalarExpr(E->getArg(0));
+  const auto *OutArg1 = dyn_cast<HLSLOutArgExpr>(E->getArg(1));
+  const auto *OutArg2 = dyn_cast<HLSLOutArgExpr>(E->getArg(2));
+
+  CallArgList Args;
+  LValue Op1TmpLValue =
+      CGF->EmitHLSLOutArgExpr(OutArg1, Args, OutArg1->getType());
+  LValue Op2TmpLValue =
+      CGF->EmitHLSLOutArgExpr(OutArg2, Args, OutArg2->getType());
+
+  if (CGF->getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee())
+    Args.reverseWritebacks();
+
+  auto EmitVectorCode =
+      [](Value *Op, CGBuilderTy *Builder,
+         FixedVectorType *DestTy) -> std::pair<Value *, Value *> {
+    Value *bitcast = Builder->CreateBitCast(Op, DestTy);
+
+    SmallVector<int> LowbitsIndex;
+    SmallVector<int> HighbitsIndex;
+
+    for (unsigned int Idx = 0; Idx < DestTy->getNumElements(); Idx += 2) {
+      LowbitsIndex.push_back(Idx);
+      HighbitsIndex.push_back(Idx + 1);
+    }
+
+    Value *Arg0 = Builder->CreateShuffleVector(bitcast, LowbitsIndex);
+    Value *Arg1 = Builder->CreateShuffleVector(bitcast, HighbitsIndex);
+
+    return std::make_pair(Arg0, Arg1);
+  };
----------------
tex3d wrote:

Since this lambda function is only used in the SPIR-V path, it could probably be defined in the else block of `if (CGF->CGM.getTarget().getTriple().isDXIL())`.

Also, this function looks more general than it is.  It can only handle one case, `DestTy` of `<4 x i32>`, since you have to do things differently for scalar double cast (using extractelement instead of shuffle).  Since that's the case, you don't need a general loop to construct indices, you can make the name more specific and clearer, like `EmitDouble2Cast`, and you don't need to supply a `DestTy`, since it's always the same cast type.

```suggestion
  // casts `<2 x double>` to `<4 x i32>`, then shuffles into high and low
  // `<2 x i32>` vectors.
  auto EmitDouble2Cast = [](CodeGenFunction &CGF,
                            Value *DoubleVec2) -> std::pair<Value *, Value *> {
    Value *BC = CGF.Builder.CreateBitCast(DoubleVec2,
                                          FixedVectorType::get(CGF.Int32Ty, 4));
    Value *LB = CGF.Builder.CreateShuffleVector(BC, {0, 2});
    Value *HB = CGF.Builder.CreateShuffleVector(BC, {1, 3});

    return std::make_pair(LB, HB);
  };
```

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


More information about the cfe-commits mailing list