[clang] [WIP][libc++] Add builtin to clear padding bytes (prework for P0528R3) (PR #75371)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 13 11:43:45 PST 2023
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff b3e80d8ed251bfdad4a49fee19b8354eba407d1d ff8a6cad88519dec919451c37bba03aa6ef21324 -- clang/test/CodeGenCXX/builtin-zero-non-value-bits-codegen.cpp clang/test/CodeGenCXX/builtin-zero-non-value-bits.cpp clang/test/SemaCXX/builtin-zero-non-value-bits.cpp clang/lib/CodeGen/CGBuiltin.cpp clang/lib/Sema/SemaChecking.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 638af25fb7..6cae75ef9d 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -63,8 +63,6 @@
#include <optional>
#include <sstream>
-
-
#include <iostream>
using namespace clang;
@@ -2461,17 +2459,20 @@ static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF,
}
template <class T>
-void RecursivelyClearPaddingImpl(CodeGenFunction &CGF, Value *Ptr, QualType Ty, size_t CurrentStartOffset, size_t &RunningOffset, T&& WriteZeroAtOffset);
+void RecursivelyClearPaddingImpl(CodeGenFunction &CGF, Value *Ptr, QualType Ty,
+ size_t CurrentStartOffset,
+ size_t &RunningOffset, T &&WriteZeroAtOffset);
template <class T>
-void ClearPaddingStruct(CodeGenFunction &CGF, Value *Ptr, QualType Ty, StructType *ST,
- size_t CurrentStartOffset, size_t &RunningOffset, T&& WriteZeroAtOffset) {
+void ClearPaddingStruct(CodeGenFunction &CGF, Value *Ptr, QualType Ty,
+ StructType *ST, size_t CurrentStartOffset,
+ size_t &RunningOffset, T &&WriteZeroAtOffset) {
std::cerr << "\n struct! " << ST->getName().data() << std::endl;
auto SL = CGF.CGM.getModule().getDataLayout().getStructLayout(ST);
auto R = cast<CXXRecordDecl>(Ty->getAsRecordDecl());
const ASTRecordLayout &ASTLayout = CGF.getContext().getASTRecordLayout(R);
for (auto Base : R->bases()) {
- std::cerr << "\n\n base!" << std::endl;
+ std::cerr << "\n\n base!" << std::endl;
// Zero padding between base elements.
auto BaseRecord = cast<CXXRecordDecl>(Base.getType()->getAsRecordDecl());
auto Offset = static_cast<size_t>(
@@ -2481,7 +2482,9 @@ void ClearPaddingStruct(CodeGenFunction &CGF, Value *Ptr, QualType Ty, StructTyp
Value *Idx = CGF.Builder.getSize(Index);
llvm::Type *CurrentBaseType = CGF.ConvertTypeForMem(Base.getType());
Value *BaseElement = CGF.Builder.CreateGEP(CurrentBaseType, Ptr, Idx);
- RecursivelyClearPaddingImpl(CGF, BaseElement, Base.getType(), CurrentStartOffset + Offset, RunningOffset, WriteZeroAtOffset);
+ RecursivelyClearPaddingImpl(CGF, BaseElement, Base.getType(),
+ CurrentStartOffset + Offset, RunningOffset,
+ WriteZeroAtOffset);
}
size_t NumFields = std::distance(R->field_begin(), R->field_end());
@@ -2491,68 +2494,81 @@ void ClearPaddingStruct(CodeGenFunction &CGF, Value *Ptr, QualType Ty, StructTyp
auto Offset = ASTLayout.getFieldOffset(I) / 8;
auto Index = SL->getElementContainingOffset(Offset);
Value *Idx = CGF.Builder.getSize(Index);
- llvm::Type *CurrentFieldType = CGF.ConvertTypeForMem(CurrentField->getType());
+ llvm::Type *CurrentFieldType =
+ CGF.ConvertTypeForMem(CurrentField->getType());
Value *Element = CGF.Builder.CreateGEP(CurrentFieldType, Ptr, Idx);
- RecursivelyClearPaddingImpl(CGF, Element, CurrentField->getType(), CurrentStartOffset + Offset, RunningOffset, WriteZeroAtOffset);
+ RecursivelyClearPaddingImpl(CGF, Element, CurrentField->getType(),
+ CurrentStartOffset + Offset, RunningOffset,
+ WriteZeroAtOffset);
}
}
template <class T>
-void ClearPaddingConstantArray(CodeGenFunction &CGF, Value *Ptr, llvm::Type *Type, ConstantArrayType const *AT,
- size_t CurrentStartOffset, size_t &RunningOffset, T&& WriteZeroAtOffset) {
+void ClearPaddingConstantArray(CodeGenFunction &CGF, Value *Ptr,
+ llvm::Type *Type, ConstantArrayType const *AT,
+ size_t CurrentStartOffset, size_t &RunningOffset,
+ T &&WriteZeroAtOffset) {
for (size_t ArrIndex = 0; ArrIndex < AT->getSize().getLimitedValue();
++ArrIndex) {
QualType ElementQualType = AT->getElementType();
auto *ElementRecord = ElementQualType->getAsRecordDecl();
- if(!ElementRecord){
- std::cerr<< "\n\n null!" << std::endl;
+ if (!ElementRecord) {
+ std::cerr << "\n\n null!" << std::endl;
}
- auto ElementAlign =ElementRecord?
- CGF.getContext().getASTRecordLayout(ElementRecord).getAlignment():
- CGF.getContext().getTypeAlignInChars(ElementQualType);
+ auto ElementAlign =
+ ElementRecord
+ ? CGF.getContext().getASTRecordLayout(ElementRecord).getAlignment()
+ : CGF.getContext().getTypeAlignInChars(ElementQualType);
+
+ std::cerr << "\n\n align: " << ElementAlign.getQuantity() << std::endl;
- std::cerr<< "\n\n align: " << ElementAlign.getQuantity() << std::endl;
-
// Value *Idx = CGF.Builder.getSize(0);
// Value *ArrayElement = CGF.Builder.CreateGEP(ElementType, Ptr, Idx);
Address FieldElementAddr{Ptr, Type, ElementAlign};
- auto Element =
- CGF.Builder.CreateConstArrayGEP(FieldElementAddr, ArrIndex);
+ auto Element = CGF.Builder.CreateConstArrayGEP(FieldElementAddr, ArrIndex);
auto *ElementType = CGF.ConvertTypeForMem(ElementQualType);
- auto AllocSize = CGF.CGM.getModule().getDataLayout().getTypeAllocSize(ElementType);
+ auto AllocSize =
+ CGF.CGM.getModule().getDataLayout().getTypeAllocSize(ElementType);
std::cerr << "\n\n clearing array index! " << ArrIndex << std::endl;
- RecursivelyClearPaddingImpl(CGF, Element.getPointer(), ElementQualType, CurrentStartOffset + ArrIndex * AllocSize.getKnownMinValue(), RunningOffset, WriteZeroAtOffset);
+ RecursivelyClearPaddingImpl(CGF, Element.getPointer(), ElementQualType,
+ CurrentStartOffset +
+ ArrIndex * AllocSize.getKnownMinValue(),
+ RunningOffset, WriteZeroAtOffset);
}
}
template <class T>
-void RecursivelyClearPaddingImpl(CodeGenFunction &CGF, Value *Ptr, QualType Ty, size_t CurrentStartOffset,
- size_t& RunningOffset, T&& WriteZeroAtOffset) {
+void RecursivelyClearPaddingImpl(CodeGenFunction &CGF, Value *Ptr, QualType Ty,
+ size_t CurrentStartOffset,
+ size_t &RunningOffset, T &&WriteZeroAtOffset) {
- std::cerr << "\n\n zero padding before current ["<< RunningOffset << ", " << CurrentStartOffset<< ")"<< std::endl;
+ std::cerr << "\n\n zero padding before current [" << RunningOffset << ", "
+ << CurrentStartOffset << ")" << std::endl;
for (; RunningOffset < CurrentStartOffset; ++RunningOffset) {
WriteZeroAtOffset(RunningOffset);
}
auto Type = CGF.ConvertTypeForMem(Ty);
auto Size = CGF.CGM.getModule()
- .getDataLayout()
- .getTypeSizeInBits(Type)
- .getKnownMinValue() / 8;
+ .getDataLayout()
+ .getTypeSizeInBits(Type)
+ .getKnownMinValue() /
+ 8;
if (auto *AT = dyn_cast<ConstantArrayType>(Ty)) {
- ClearPaddingConstantArray(CGF, Ptr, Type, AT, CurrentStartOffset, RunningOffset, WriteZeroAtOffset);
- }
- else if (auto *ST = dyn_cast<StructType>(Type)) {
- ClearPaddingStruct(CGF, Ptr, Ty, ST, CurrentStartOffset, RunningOffset, WriteZeroAtOffset);
+ ClearPaddingConstantArray(CGF, Ptr, Type, AT, CurrentStartOffset,
+ RunningOffset, WriteZeroAtOffset);
+ } else if (auto *ST = dyn_cast<StructType>(Type)) {
+ ClearPaddingStruct(CGF, Ptr, Ty, ST, CurrentStartOffset, RunningOffset,
+ WriteZeroAtOffset);
} else {
- std::cerr << "\n\n increment running offset from: " << RunningOffset << " to " << RunningOffset + Size << std::endl;
+ std::cerr << "\n\n increment running offset from: " << RunningOffset
+ << " to " << RunningOffset + Size << std::endl;
RunningOffset += Size;
}
-
}
static void RecursivelyZeroNonValueBits(CodeGenFunction &CGF, Value *Ptr,
@@ -2567,20 +2583,22 @@ static void RecursivelyZeroNonValueBits(CodeGenFunction &CGF, Value *Ptr,
CharUnits::One().alignmentAtOffset(CharUnits::fromQuantity(Offset)));
};
-
size_t RunningOffset = 0;
- RecursivelyClearPaddingImpl(CGF, Ptr, Ty, 0, RunningOffset, WriteZeroAtOffset);
+ RecursivelyClearPaddingImpl(CGF, Ptr, Ty, 0, RunningOffset,
+ WriteZeroAtOffset);
// Clear tail padding
auto Type = CGF.ConvertTypeForMem(Ty);
-
+
auto Size = CGF.CGM.getModule()
- .getDataLayout()
- .getTypeSizeInBits(Type)
- .getKnownMinValue() / 8;
+ .getDataLayout()
+ .getTypeSizeInBits(Type)
+ .getKnownMinValue() /
+ 8;
- std::cerr << "\n\n zero tail padding ["<< RunningOffset << ", " << Size << ")"<< std::endl;
+ std::cerr << "\n\n zero tail padding [" << RunningOffset << ", " << Size
+ << ")" << std::endl;
for (; RunningOffset < Size; ++RunningOffset) {
WriteZeroAtOffset(RunningOffset);
}
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3470dfca3e..d923164478 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2333,7 +2333,8 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
// if (!PtrArgType->isPointerType() ||
// !PtrArgType->getPointeeType()->isRecordType()) {
// Diag(PtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
- // << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 << PtrArgType
+ // << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 <<
+ // PtrArgType
// << "structure pointer";
// return ExprError();
// }
@@ -2342,7 +2343,8 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
// << TheCall->getSourceRange() << 5 /*ConstUnknown*/;
// return ExprError();
// }
- // if (RequireCompleteType(PtrArg->getBeginLoc(), PtrArgType->getPointeeType(),
+ // if (RequireCompleteType(PtrArg->getBeginLoc(),
+ // PtrArgType->getPointeeType(),
// diag::err_typecheck_decl_incomplete_type))
// return ExprError();
break;
``````````
</details>
https://github.com/llvm/llvm-project/pull/75371
More information about the cfe-commits
mailing list