[clang] [CIR] Upstream AssignmentMemcpyizer (PR #236803)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 07:46:55 PST 2026
================
@@ -24,6 +24,273 @@
using namespace clang;
using namespace clang::CIRGen;
+namespace {
+// TODO(cir): a lot of what we see under this namespace is a strong candidate
+// to be shared between LLVM and CIR codegen.
+
+/// RAII object to indicate that codegen is copying the value representation
+/// instead of the object representation. Useful when copying a struct or
+/// class which has uninitialized members and we're only performing
+/// lvalue-to-rvalue conversion on the object but not its members.
+class CopyingValueRepresentation {
+public:
+ explicit CopyingValueRepresentation(CIRGenFunction &cgf)
+ : cgf(cgf), oldSanOpts(cgf.sanOpts) {
+ cgf.sanOpts.set(SanitizerKind::Bool, false);
+ cgf.sanOpts.set(SanitizerKind::Enum, false);
+ }
+ ~CopyingValueRepresentation() { cgf.sanOpts = oldSanOpts; }
+
+private:
+ CIRGenFunction &cgf;
+ SanitizerSet oldSanOpts;
+};
+
+class FieldMemcpyizer {
+public:
+ FieldMemcpyizer(CIRGenFunction &cgf, const CXXMethodDecl *methodDecl,
+ const VarDecl *srcRec)
+ : cgf(cgf), methodDecl(methodDecl), classDecl(methodDecl->getParent()),
+ srcRec(srcRec),
+ recLayout(cgf.getContext().getASTRecordLayout(classDecl)),
+ firstField(nullptr), lastField(nullptr), firstFieldOffset(0),
+ lastFieldOffset(0), lastAddedFieldIndex(0) {}
+
+ bool isMemcpyableField(FieldDecl *f) const {
+ // Never memcpy fields when we are adding poised paddings.
+ if (cgf.getContext().getLangOpts().SanitizeAddressFieldPadding)
+ return false;
+ Qualifiers qual = f->getType().getQualifiers();
+ if (qual.hasVolatile() || qual.hasObjCLifetime())
+ return false;
+ return true;
+ }
+
+ void addMemcpyableField(FieldDecl *f) {
+ // TODO(cir): isEmptyFieldForLayout is not handled here yet.
+ // Incubator has assert(!cir::MissingFeatures::isEmptyFieldForLayout());
----------------
Fznamznon wrote:
Yep, I was able to add the check but not the test for it. Incubator's `getMemcpyableField` intentionally (there is a comment) exits when it meets an operator call so something that would trigger `isEmptyFieldForLayout` (i.e. an empty struct) never reaches `addMemcpyableField` . So that early return is basically dead code now, let me know if I'm missing something and whether we want to keep it at all.
https://github.com/llvm/llvm-project/pull/236803
More information about the cfe-commits
mailing list