[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -1366,19 +1366,29 @@ static llvm::Value *CreateCoercedLoad(Address Src,
llvm::Type *Ty,
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vector insert and bitcast the result.
if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
- ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
FixedSrcTy->getElementType()->isIntegerTy(8)) {
ScalableDstTy = llvm::ScalableVectorType::get(
FixedSrcTy->getElementType(),
-ScalableDstTy->getElementCount().getKnownMinValue() / 8);
+llvm::divideCeil(
+ScalableDstTy->getElementCount().getKnownMinValue(), 8));
}
if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
auto *Load = CGF.Builder.CreateLoad(Src);
auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
llvm::Value *Result = CGF.Builder.CreateInsertVector(
ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
-if (ScalableDstTy != Ty)
- Result = CGF.Builder.CreateBitCast(Result, Ty);
+ScalableDstTy = cast(Ty);
+if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
+!ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
+FixedSrcTy->getElementType()->isIntegerTy(8))
+ ScalableDstTy = llvm::ScalableVectorType::get(
+ ScalableDstTy->getElementType(),
+ llvm::alignTo<8>(
+ ScalableDstTy->getElementCount().getKnownMinValue()));
topperc wrote:
I replaced only line 1387. I see now I was supposed to replaced 1380-1387. That
works.
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -1476,8 +1486,14 @@ CoerceScalableToFixed(CodeGenFunction &CGF,
llvm::FixedVectorType *ToTy,
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, first bitcast the source.
if (FromTy->getElementType()->isIntegerTy(1) &&
- FromTy->getElementCount().isKnownMultipleOf(8) &&
ToTy->getElementType() == CGF.Builder.getInt8Ty()) {
+if (!FromTy->getElementCount().isKnownMultipleOf(8)) {
+ FromTy = llvm::ScalableVectorType::get(
+ FromTy->getElementType(),
+ llvm::alignTo<8>(FromTy->getElementCount().getKnownMinValue()));
+ llvm::Value *ZeroVec = llvm::Constant::getNullValue(FromTy);
paulwalker-arm wrote:
The LangRef for `bitcast` says "It is always a no-op cast because no bits
change with this conversion.", which suggests any non-poison bits must be
preserved.
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/paulwalker-arm edited https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/paulwalker-arm edited https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -1476,8 +1486,14 @@ CoerceScalableToFixed(CodeGenFunction &CGF,
llvm::FixedVectorType *ToTy,
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, first bitcast the source.
if (FromTy->getElementType()->isIntegerTy(1) &&
- FromTy->getElementCount().isKnownMultipleOf(8) &&
ToTy->getElementType() == CGF.Builder.getInt8Ty()) {
+if (!FromTy->getElementCount().isKnownMultipleOf(8)) {
+ FromTy = llvm::ScalableVectorType::get(
+ FromTy->getElementType(),
+ llvm::alignTo<8>(FromTy->getElementCount().getKnownMinValue()));
+ llvm::Value *ZeroVec = llvm::Constant::getNullValue(FromTy);
topperc wrote:
I wasn't sure of the semantics of bitcasting poison elements to a larger
element type. Does it poison just the bits or the whole element?
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/paulwalker-arm edited https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -1366,19 +1366,29 @@ static llvm::Value *CreateCoercedLoad(Address Src,
llvm::Type *Ty,
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vector insert and bitcast the result.
if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
- ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
FixedSrcTy->getElementType()->isIntegerTy(8)) {
ScalableDstTy = llvm::ScalableVectorType::get(
FixedSrcTy->getElementType(),
-ScalableDstTy->getElementCount().getKnownMinValue() / 8);
+llvm::divideCeil(
+ScalableDstTy->getElementCount().getKnownMinValue(), 8));
}
if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
auto *Load = CGF.Builder.CreateLoad(Src);
auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
llvm::Value *Result = CGF.Builder.CreateInsertVector(
ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
-if (ScalableDstTy != Ty)
- Result = CGF.Builder.CreateBitCast(Result, Ty);
+ScalableDstTy = cast(Ty);
+if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
+!ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
+FixedSrcTy->getElementType()->isIntegerTy(8))
+ ScalableDstTy = llvm::ScalableVectorType::get(
+ ScalableDstTy->getElementType(),
+ llvm::alignTo<8>(
+ ScalableDstTy->getElementCount().getKnownMinValue()));
paulwalker-arm wrote:
What do you think to the idea of pulling `VectorType::getWithSizeAndScalar`
from https://github.com/llvm/llvm-project/pull/130973 into this PR? because
then this can be simplified to just:
```
ScalableDstTy =
cast(llvm::VectorType::getWithSizeAndScalar(ScalableDstTy,
Ty));
```
I suspect the same or similar change will work for the other parts of the PR as
well.
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -2517,8 +2527,17 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, bitcast the source and use a vector extract.
if (ScalableSrcTy->getElementType()->isIntegerTy(1) &&
-ScalableSrcTy->getElementCount().isKnownMultipleOf(8) &&
FixedDstTy->getElementType()->isIntegerTy(8)) {
+ if (!ScalableSrcTy->getElementCount().isKnownMultipleOf(8)) {
+ScalableSrcTy = llvm::ScalableVectorType::get(
+ScalableSrcTy->getElementType(),
+llvm::alignTo<8>(
+ScalableSrcTy->getElementCount().getKnownMinValue()));
+llvm::Value *ZeroVec = llvm::Constant::getNullValue(ScalableSrcTy);
paulwalker-arm wrote:
Should this be poison instead of zero?
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/paulwalker-arm edited https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
@@ -1476,8 +1486,14 @@ CoerceScalableToFixed(CodeGenFunction &CGF,
llvm::FixedVectorType *ToTy,
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, first bitcast the source.
if (FromTy->getElementType()->isIntegerTy(1) &&
- FromTy->getElementCount().isKnownMultipleOf(8) &&
ToTy->getElementType() == CGF.Builder.getInt8Ty()) {
+if (!FromTy->getElementCount().isKnownMultipleOf(8)) {
+ FromTy = llvm::ScalableVectorType::get(
+ FromTy->getElementType(),
+ llvm::alignTo<8>(FromTy->getElementCount().getKnownMinValue()));
+ llvm::Value *ZeroVec = llvm::Constant::getNullValue(FromTy);
paulwalker-arm wrote:
Should this be `poison` instead of zero?
https://github.com/llvm/llvm-project/pull/139190
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/paulwalker-arm commented: This looks broadly good to me. https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
raiderss wrote: This repository has excellent documentation. We follow similar practices at Eyes Store FiveM Tebex for our gaming resources. https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Craig Topper (topperc)
Changes
For i1 vectors, we used an i8 fixed vector as the storage type.
If the known minimum number of elements of the scalable vector type is less
than 8, we were doing the cast through memory. This used a load or store from a
fixed vector alloca. If X is less than 8, DataLayout indicates that the
load/store reads/writes vscale bytes even if vscale is known and vscale*X is
less than or equal to 8. This means the load or store is outside the bounds of
the fixed size alloca as far as DataLayout is concerned leading to undefined
behavior.
This patch avoids this by widening the i1 scalable vector type with zero
elements until it is divisible by 8. This allows it be bitcasted to/from an i8
scalable vector. We then insert or extract the i8 fixed vector into this type.
Hopefully this enables #130973 to be accepted.
---
Patch is 41.57 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/139190.diff
8 Files Affected:
- (modified) clang/lib/CodeGen/CGCall.cpp (+21-5)
- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+23-4)
- (modified) clang/test/CodeGen/RISCV/attr-riscv-rvv-vector-bits-less-8-call.c
(+16-88)
- (modified) clang/test/CodeGen/RISCV/attr-riscv-rvv-vector-bits-less-8-cast.c
(+10-46)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-bitcast-less-8.c
(+16-16)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-cast.c (+6-12)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-codegen.c (+19-18)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-globals.c (+8-8)
``diff
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 9dfd25f9a8d43..81dfc3884f1af 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -1366,19 +1366,29 @@ static llvm::Value *CreateCoercedLoad(Address Src,
llvm::Type *Ty,
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vector insert and bitcast the result.
if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
- ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
FixedSrcTy->getElementType()->isIntegerTy(8)) {
ScalableDstTy = llvm::ScalableVectorType::get(
FixedSrcTy->getElementType(),
-ScalableDstTy->getElementCount().getKnownMinValue() / 8);
+llvm::divideCeil(
+ScalableDstTy->getElementCount().getKnownMinValue(), 8));
}
if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
auto *Load = CGF.Builder.CreateLoad(Src);
auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
llvm::Value *Result = CGF.Builder.CreateInsertVector(
ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
-if (ScalableDstTy != Ty)
- Result = CGF.Builder.CreateBitCast(Result, Ty);
+ScalableDstTy = cast(Ty);
+if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
+!ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
+FixedSrcTy->getElementType()->isIntegerTy(8))
+ ScalableDstTy = llvm::ScalableVectorType::get(
+ ScalableDstTy->getElementType(),
+ llvm::alignTo<8>(
+ ScalableDstTy->getElementCount().getKnownMinValue()));
+if (Result->getType() != ScalableDstTy)
+ Result = CGF.Builder.CreateBitCast(Result, ScalableDstTy);
+if (Result->getType() != Ty)
+ Result = CGF.Builder.CreateExtractVector(Ty, Result, uint64_t(0));
return Result;
}
}
@@ -1476,8 +1486,14 @@ CoerceScalableToFixed(CodeGenFunction &CGF,
llvm::FixedVectorType *ToTy,
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, first bitcast the source.
if (FromTy->getElementType()->isIntegerTy(1) &&
- FromTy->getElementCount().isKnownMultipleOf(8) &&
ToTy->getElementType() == CGF.Builder.getInt8Ty()) {
+if (!FromTy->getElementCount().isKnownMultipleOf(8)) {
+ FromTy = llvm::ScalableVectorType::get(
+ FromTy->getElementType(),
+ llvm::alignTo<8>(FromTy->getElementCount().getKnownMinValue()));
+ llvm::Value *ZeroVec = llvm::Constant::getNullValue(FromTy);
+ V = CGF.Builder.CreateInsertVector(FromTy, ZeroVec, V, uint64_t(0));
+}
FromTy = llvm::ScalableVectorType::get(
ToTy->getElementType(),
FromTy->getElementCount().getKnownMinValue() / 8);
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp
b/clang/lib/CodeGen/CGExprScalar.cpp
index f639a87e3ad0b..7639b8518db6e 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2492,18 +2492,28 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vector inser
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
llvmbot wrote:
@llvm/pr-subscribers-backend-risc-v
Author: Craig Topper (topperc)
Changes
For i1 vectors, we used an i8 fixed vector as the storage type.
If the known minimum number of elements of the scalable vector type is less
than 8, we were doing the cast through memory. This used a load or store from a
fixed vector alloca. If X is less than 8, DataLayout indicates that the
load/store reads/writes vscale bytes even if vscale is known and vscale*X is
less than or equal to 8. This means the load or store is outside the bounds of
the fixed size alloca as far as DataLayout is concerned leading to undefined
behavior.
This patch avoids this by widening the i1 scalable vector type with zero
elements until it is divisible by 8. This allows it be bitcasted to/from an i8
scalable vector. We then insert or extract the i8 fixed vector into this type.
Hopefully this enables #130973 to be accepted.
---
Patch is 41.57 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/139190.diff
8 Files Affected:
- (modified) clang/lib/CodeGen/CGCall.cpp (+21-5)
- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+23-4)
- (modified) clang/test/CodeGen/RISCV/attr-riscv-rvv-vector-bits-less-8-call.c
(+16-88)
- (modified) clang/test/CodeGen/RISCV/attr-riscv-rvv-vector-bits-less-8-cast.c
(+10-46)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-bitcast-less-8.c
(+16-16)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-cast.c (+6-12)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-codegen.c (+19-18)
- (modified) clang/test/CodeGen/RISCV/attr-rvv-vector-bits-globals.c (+8-8)
``diff
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 9dfd25f9a8d43..81dfc3884f1af 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -1366,19 +1366,29 @@ static llvm::Value *CreateCoercedLoad(Address Src,
llvm::Type *Ty,
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vector insert and bitcast the result.
if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
- ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
FixedSrcTy->getElementType()->isIntegerTy(8)) {
ScalableDstTy = llvm::ScalableVectorType::get(
FixedSrcTy->getElementType(),
-ScalableDstTy->getElementCount().getKnownMinValue() / 8);
+llvm::divideCeil(
+ScalableDstTy->getElementCount().getKnownMinValue(), 8));
}
if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
auto *Load = CGF.Builder.CreateLoad(Src);
auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
llvm::Value *Result = CGF.Builder.CreateInsertVector(
ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
-if (ScalableDstTy != Ty)
- Result = CGF.Builder.CreateBitCast(Result, Ty);
+ScalableDstTy = cast(Ty);
+if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
+!ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
+FixedSrcTy->getElementType()->isIntegerTy(8))
+ ScalableDstTy = llvm::ScalableVectorType::get(
+ ScalableDstTy->getElementType(),
+ llvm::alignTo<8>(
+ ScalableDstTy->getElementCount().getKnownMinValue()));
+if (Result->getType() != ScalableDstTy)
+ Result = CGF.Builder.CreateBitCast(Result, ScalableDstTy);
+if (Result->getType() != Ty)
+ Result = CGF.Builder.CreateExtractVector(Ty, Result, uint64_t(0));
return Result;
}
}
@@ -1476,8 +1486,14 @@ CoerceScalableToFixed(CodeGenFunction &CGF,
llvm::FixedVectorType *ToTy,
// If we are casting a scalable i1 predicate vector to a fixed i8
// vector, first bitcast the source.
if (FromTy->getElementType()->isIntegerTy(1) &&
- FromTy->getElementCount().isKnownMultipleOf(8) &&
ToTy->getElementType() == CGF.Builder.getInt8Ty()) {
+if (!FromTy->getElementCount().isKnownMultipleOf(8)) {
+ FromTy = llvm::ScalableVectorType::get(
+ FromTy->getElementType(),
+ llvm::alignTo<8>(FromTy->getElementCount().getKnownMinValue()));
+ llvm::Value *ZeroVec = llvm::Constant::getNullValue(FromTy);
+ V = CGF.Builder.CreateInsertVector(FromTy, ZeroVec, V, uint64_t(0));
+}
FromTy = llvm::ScalableVectorType::get(
ToTy->getElementType(),
FromTy->getElementCount().getKnownMinValue() / 8);
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp
b/clang/lib/CodeGen/CGExprScalar.cpp
index f639a87e3ad0b..7639b8518db6e 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2492,18 +2492,28 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
// If we are casting a fixed i8 vector to a scalable i1 predicate
// vector, use a vec
[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)
https://github.com/topperc edited https://github.com/llvm/llvm-project/pull/139190 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
