Author: Yingwei Zheng Date: 2025-04-10T00:48:18+08:00 New Revision: c0480738cb3f4b2db47b77f5b18f9819cb995373
URL: https://github.com/llvm/llvm-project/commit/c0480738cb3f4b2db47b77f5b18f9819cb995373 DIFF: https://github.com/llvm/llvm-project/commit/c0480738cb3f4b2db47b77f5b18f9819cb995373.diff LOG: [Clang][CodeGen] Respect -fwrapv-pointer when emitting struct GEPs (#134269) This patch turns off inbounds/nuw flags for member accesses when `-fwrapv-pointer` is set. Closes https://github.com/llvm/llvm-project/issues/132449. It is required by https://github.com/llvm/llvm-project/pull/130734. Added: Modified: clang/lib/CodeGen/CGExpr.cpp clang/test/CodeGen/pointer-overflow.c Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index de7c577a23493..451034395976f 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -4915,6 +4915,9 @@ static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, unsigned idx = CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); + if (CGF.getLangOpts().PointerOverflowDefined) + return CGF.Builder.CreateConstGEP2_32(base, 0, idx, field->getName()); + return CGF.Builder.CreateStructGEP(base, idx, field->getName()); } @@ -4972,9 +4975,13 @@ LValue CodeGenFunction::EmitLValueForField(LValue base, if (!UseVolatile) { if (!IsInPreservedAIRegion && (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) { - if (Idx != 0) + if (Idx != 0) { // For structs, we GEP to the field that the record layout suggests. - Addr = Builder.CreateStructGEP(Addr, Idx, field->getName()); + if (getLangOpts().PointerOverflowDefined) + Addr = Builder.CreateConstGEP2_32(Addr, 0, Idx, field->getName()); + else + Addr = Builder.CreateStructGEP(Addr, Idx, field->getName()); + } } else { llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType( getContext().getRecordType(rec), rec->getLocation()); diff --git a/clang/test/CodeGen/pointer-overflow.c b/clang/test/CodeGen/pointer-overflow.c index 9c7821b841980..70e3c855bff90 100644 --- a/clang/test/CodeGen/pointer-overflow.c +++ b/clang/test/CodeGen/pointer-overflow.c @@ -10,3 +10,24 @@ void test(void) { // DEFAULT: getelementptr inbounds nuw i32, ptr // FWRAPV-POINTER: getelementptr i32, ptr } + +struct S { + int a; + int b; + int c: 10; + int d: 10; +}; + +int test_struct(struct S* s) { + // -fwrapv-pointer should turn off inbounds nuw for struct GEP's + return s->b; + // DEFAULT: getelementptr inbounds nuw %struct.S, ptr + // FWRAPV-POINTER: getelementptr %struct.S, ptr +} + +int test_struct_bitfield(struct S* s) { + // -fwrapv-pointer should turn off inbounds nuw for struct GEP's + return s->d; + // DEFAULT: getelementptr inbounds nuw %struct.S, ptr + // FWRAPV-POINTER: getelementptr %struct.S, ptr +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits