llvm / llvm/llvm-project

Refactor `CodeGenFunction::EmitStoreThroughLValue` in `CGExpr.cpp` to consolidate HLSL-specific behavior into helper functions

Open
#176,468 6 comments 0 reactions 1 assignee Claimed by @emias11 View on GitHub
clang:codegen good first issue HLSL
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

As pointed out by @farzonl in [#176216](https://github.com/llvm/llvm-project/pull/176216/changes/BASE..13d849da4710f37710e909699c6150d7a52ec851#r2699745999) the `CodeGenFunction::EmitStoreThroughLValue` function in `CGExpr.cpp` has three significantly large HLSL-specific if-statements for handling stores through LValues of types `Dst.isVectorElt()`, `Dst.isMatrixElt()`, and `Dst.isMatrixRow()`.

The HLSL-specific behaviors essentially boil down to:
- Zero-extending the Src to its corresponding type for the given destination Address
```c++
llvm::Value *Val = Src.getScalarVal();
if (Val->getType()->getPrimitiveSizeInBits() <
ElemTy->getScalarSizeInBits())
Val = Builder.CreateZExt(Val, ElemTy->getScalarType());
```
- Storing individual elements of vectors/matrices into memory via a vector GEP followed by a scalar store, as opposed to storing the entire vector/matrix as one unit
```c++
llvm::Value *Idx = Dst.getVectorIdx();
llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
Address DstElemAddr =
Builder.CreateGEP(DstAddr, {Zero, Idx}, DestAddrTy, ElemAlign);
Builder.CreateStore(Val, DstElemAddr, Dst.isVolatileQualified());
```

It would be preferrable to have these behaviors consolidated into their own (HLSL-specific) helper functions, which should not only make the code easier to read by reducing code duplication, but also potentially make it easier for other language modes to adopt the use of one or both of these behaviors.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.