[clang-tools-extra] [clang] [llvm] [CodeGen] Revamp counted_by calculations (PR #70606)

2023-11-13 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This change introduces a crash with `-fsanitize=array-bounds`. A reproducer 
from `cvise`:

```c
struct irq_data {
  struct irq_domain *domain;
} irq_domain_fix_revmap_d;
struct irq_domain {
  struct irq_domain *parent;
  int revmap_size;
  struct irq_data *revmap[] __attribute__((__counted_by__(revmap_size)));
};
long irq_domain_fix_revmap_d_0;
int irq_domain_pop_irq() {
  irq_domain_fix_revmap_d.domain->revmap[irq_domain_fix_revmap_d_0] = 0;
  return 0;
}
```

```
clang: 
/mnt/nvme/tmp/cvise.buvTN27aMk/src/llvm/include/llvm/IR/DataLayout.h:652: 
TypeSize llvm::StructLayout::getElementOffset(unsigned int) const: Assertion 
`Idx < NumElements && "Invalid element idx!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: clang -O2 -fsanitize=array-bounds -c -o /dev/null 
irqdomain.i
1.   parser at end of file
2.  irqdomain.i:10:5: LLVM IR generation of declaration 'irq_domain_pop_irq'
3.  irqdomain.i:10:5: Generating code for declaration 'irq_domain_pop_irq'
 #0 0x5622f687d9e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x41069e8)
 #1 0x5622f687b61e llvm::sys::RunSignalHandlers() 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x410461e)
 #2 0x5622f6800926 CrashRecoverySignalHandler(int) 
CrashRecoveryContext.cpp:0:0
 #3 0x7f1204079710 (/usr/lib/libc.so.6+0x3e710)
 #4 0x7f12040c983c (/usr/lib/libc.so.6+0x8e83c)
 #5 0x7f1204079668 gsignal (/usr/lib/libc.so.6+0x3e668)
 #6 0x7f12040614b8 abort (/usr/lib/libc.so.6+0x264b8)
 #7 0x7f12040613dc (/usr/lib/libc.so.6+0x263dc)
 #8 0x7f1204071d26 (/usr/lib/libc.so.6+0x36d26)
 #9 0x5622f6ae11bb 
clang::CodeGen::CGBuilderTy::CreateStructGEP(clang::CodeGen::Address, unsigned 
int, llvm::Twine const&) CGCall.cpp:0:0
#10 0x5622f6bcd204 emitAddrOfFieldStorage(clang::CodeGen::CodeGenFunction&, 
clang::CodeGen::Address, clang::FieldDecl const*) CGExpr.cpp:0:0
#11 0x5622f6bb0082 
clang::CodeGen::CodeGenFunction::EmitLValueForField(clang::CodeGen::LValue, 
clang::FieldDecl const*) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4439082)
#12 0x5622f6bbef07 
clang::CodeGen::CodeGenFunction::EmitMemberExpr(clang::MemberExpr const*) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4447f07)
#13 0x5622f6bb7c9f 
clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, 
clang::CodeGen::KnownNonNull_t) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4440c9f)
#14 0x5622f6bb62ad 
clang::CodeGen::CodeGenFunction::EmitCheckedLValue(clang::Expr const*, 
clang::CodeGen::CodeGenFunction::TypeCheckKind) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x443f2ad)
#15 0x5622f6be69ed (anonymous 
namespace)::ScalarExprEmitter::VisitMemberExpr(clang::MemberExpr*) 
CGExprScalar.cpp:0:0
#16 0x5622f6bd2fad 
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x445bfad)
#17 0x5622f6baba93 clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr 
const*, clang::CodeGen::AggValueSlot, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4434a93)
#18 0x5622f6bac39d 
clang::CodeGen::CodeGenFunction::EmitAnyExprToTemp(clang::Expr const*) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x443539d)
#19 0x5622f6bb481f 
clang::CodeGen::CodeGenFunction::EmitBoundsCheck(clang::Expr const*, 
clang::Expr const*, llvm::Value*, clang::QualType, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x443d81f)
#20 0x5622f6bcb0e3 
clang::CodeGen::CodeGenFunction::EmitArraySubscriptExpr(clang::ArraySubscriptExpr
 const*, bool)::$_0::operator()(bool) const CGExpr.cpp:0:0
#21 0x5622f6bb7286 
clang::CodeGen::CodeGenFunction::EmitArraySubscriptExpr(clang::ArraySubscriptExpr
 const*, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4440286)
#22 0x5622f6bb629b 
clang::CodeGen::CodeGenFunction::EmitCheckedLValue(clang::Expr const*, 
clang::CodeGen::CodeGenFunction::TypeCheckKind) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x443f29b)
#23 0x5622f6bdf6cb (anonymous 
namespace)::ScalarExprEmitter::VisitBinAssign(clang::BinaryOperator const*) 
CGExprScalar.cpp:0:0
#24 0x5622f6bd2fad 
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x445bfad)
#25 0x5622f6baba93 clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr 
const*, clang::CodeGen::AggValueSlot, bool) 
(/mnt/nvme/tmp/cvise.buvTN27aMk/install/llvm-bad/bin/clang-18+0x4434a93)
#26 0x5622f6baba1c 
clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) 
(/mnt/n

[clang] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

2023-12-05 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

For what it's worth, I see a new crash on the latest version of this PR, in 
case it is not known.

```
$ make -skj"$(nproc)" ARCH=arm64 LLVM=1 mrproper allmodconfig 
net/ipv4/udp_tunnel_nic.o
clang: /home/nathan/cbl/src/llvm-project/llvm/lib/IR/Instructions.cpp:3342: 
static CastInst *llvm::CastInst::Create(Instruction::CastOps, Value *, Type *, 
const Twine &, Instruction *): Assertion `castIsValid(op, S, Ty) && "Invalid 
cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: /mnt/nvme/tmp/build/llvm-bisect/final/bin/clang 
--target=aarch64-linux-gnu -fintegrated-as -Werror=unknown-warning-option 
-Werror=ignored-optimization-argument -Werror=option-ignored 
-Werror=unused-command-line-argument -mlittle-endian -fmacro-prefix-map== 
-std=gnu11 -fshort-wchar -funsigned-char -fno-common -fno-PIE 
-fno-strict-aliasing -mgeneral-regs-only -Wno-psabi 
-fasynchronous-unwind-tables -mbranch-protection=pac-ret+bti -ffixed-x18 
-fno-delete-null-pointer-checks -O2 -fstack-protector-strong 
-fno-omit-frame-pointer -fno-optimize-sibling-calls 
-ftrivial-auto-var-init=pattern -fno-stack-clash-protection 
-fzero-call-used-regs=used-gpr -fpatchable-function-entry=2 -fsanitize=kcfi 
-falign-functions=64 -fstrict-flex-arrays=3 -fno-strict-overflow 
-fno-stack-check -Wall -Wundef -Werror=implicit-function-declaration 
-Werror=implicit-int -Werror=return-type -Werror=strict-prototypes 
-Wno-format-security -Wno-trigraphs -Wno-frame-address 
-Wno-address-of-packed-member -Wframe-larger-than=2048 -Wno-gnu 
-Wno-unused-but-set-variable -Wno-unused-const-variable -Wvla -Wno-pointer-sign 
-Wcast-function-type -Wimplicit-fallthrough -Werror=date-time 
-Werror=incompatible-pointer-types -Wenum-conversion 
-Wno-unused-but-set-variable -Wno-unused-const-variable -Wno-format-overflow 
-Wno-format-truncation -Wno-pointer-to-enum-cast 
-Wno-tautological-constant-out-of-range-compare -Wno-unaligned-access 
-Wno-cast-function-type-strict -Wno-missing-field-initializers -Wno-type-limits 
-Wno-shift-negative-value -Wno-initializer-overrides -Wno-sign-compare 
-frandomize-layout-seed-file=./scripts/basic/randstruct.seed 
-mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 
-mstack-protector-guard-offset=10432 -fsanitize=array-bounds -fsanitize=shift 
-fsanitize=unreachable -fsanitize=bool -fsanitize=enum 
-fsanitize-coverage=trace-pc -fsanitize-coverage=trace-cmp -fsanitize=thread 
-fno-optimize-sibling-calls -mllvm -tsan-compound-read-before-write=1 -mllvm 
-tsan-distinguish-volatile=1 -Werror -Wa,-march=armv8.5-a -nostdinc 
-Iarch/arm64/include -I./arch/arm64/include/generated -Iinclude -I./include 
-Iarch/arm64/include/uapi -I./arch/arm64/include/generated/uapi -Iinclude/uapi 
-I./include/generated/uapi -include include/linux/compiler-version.h -include 
include/linux/kconfig.h -include include/linux/compiler_types.h -D__KERNEL__ 
-DCC_USING_PATCHABLE_FUNCTION_ENTRY -DKASAN_SHADOW_SCALE_SHIFT= 
-DCONFIG_CC_HAS_K_CONSTRAINT=1 -DARM64_ASM_ARCH=\"armv8.5-a\" 
-DKASAN_SHADOW_SCALE_SHIFT= -DRANDSTRUCT -I net/ipv4 -I ./net/ipv4 -DMODULE 
-DKBUILD_BASENAME=\"udp_tunnel_nic\" -DKBUILD_MODNAME=\"udp_tunnel\" 
-D__KBUILD_MODNAME=kmod_udp_tunnel -c -Wp,-MMD,net/ipv4/.udp_tunnel_nic.o.d 
-fcolor-diagnostics -o net/ipv4/udp_tunnel_nic.o net/ipv4/udp_tunnel_nic.c
1.   parser at end of file
2.  Per-file LLVM IR generation
3.  net/ipv4/udp_tunnel_nic.c:345:1: Generating code for declaration 
'udp_tunnel_nic_has_collision'
4.  net/ipv4/udp_tunnel_nic.c:353:51: LLVM IR generation of compound 
statement ('{}')
 #0 0x5574960f5698 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/mnt/nvme/tmp/build/llvm-bisect/final/bin/clang+0x4153698)
 #1 0x5574960f32ce llvm::sys::RunSignalHandlers() 
(/mnt/nvme/tmp/build/llvm-bisect/final/bin/clang+0x41512ce)
 #2 0x557496077e26 CrashRecoverySignalHandler(int) 
CrashRecoveryContext.cpp:0:0
 #3 0x7f1682eaf710 (/usr/lib/libc.so.6+0x3e710)
 #4 0x7f1682eff83c (/usr/lib/libc.so.6+0x8e83c)
 #5 0x7f1682eaf668 gsignal (/usr/lib/libc.so.6+0x3e668)
 #6 0x7f1682e974b8 abort (/usr/lib/libc.so.6+0x264b8)
 #7 0x7f1682e973dc (/usr/lib/libc.so.6+0x263dc)
 #8 0x7f1682ea7d26 (/usr/lib/libc.so.6+0x36d26)
 #9 0x557495c1c9d3 llvm::CastInst::Create(llvm::Instruction::CastOps, 
llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) 
(/mnt/nvme/tmp/build/llvm-bisect/final/bin/clang+0x3c7a9d3)
#10 0x557494d032f2 llvm::IRBuilderBase::CreateIntCast(llvm::Value*, 
llvm::Type*, bool, llvm::Twine const&) AArch64TargetTransformInfo.cpp:0:0
#11 0x557496437dbf 
clang::CodeGen::CodeGenFunction::EmitBoundsCheck(clang::Expr const*, 
clang::Expr const*, llvm::Value*, clang::QualType, bool) 
(/mnt/nvme/tmp/build/llvm-bisect/final/bin/clang+0x4495dbf)
#12 0x55749644c893 
clang::CodeGen::C

[clang-tools-extra] [libc] [libcxx] [llvm] [compiler-rt] [flang] [clang] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

2023-12-18 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

The latest version of this change causes test failures for me:

```
$ cmake \
-B build \
-G Ninja \
-S llvm \
--log-level=NOTICE \
-Wno-dev \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DLLVM_ENABLE_ASSERTIONS=true \
-DLLVM_ENABLE_PROJECTS=clang \
-DLLVM_USE_LINKER=lld

$ ninja -C build check-clang
...
FAIL: Clang :: OpenMP/distribute_parallel_for_simd_misc_messages.c (12637 of 
20466)
 TEST 'Clang :: 
OpenMP/distribute_parallel_for_simd_misc_messages.c' FAILED 
Exit Code: 139

Command Output (stderr):
--
RUN: at line 1: /mnt/nvme/tmp/build/llvm-bisect/bin/clang -cc1 
-internal-isystem /mnt/nvme/tmp/build/llvm-bisect/lib/clang/18/include 
-nostdsysteminc -fsyntax-only -fopenmp -fopenmp-version=45 
-verify=expected,omp45 
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c
 -Wuninitialized
+ /mnt/nvme/tmp/build/llvm-bisect/bin/clang -cc1 -internal-isystem 
/mnt/nvme/tmp/build/llvm-bisect/lib/clang/18/include -nostdsysteminc 
-fsyntax-only -fopenmp -fopenmp-version=45 -verify=expected,omp45 
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c
 -Wuninitialized
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: /mnt/nvme/tmp/build/llvm-bisect/bin/clang -cc1 
-internal-isystem /mnt/nvme/tmp/build/llvm-bisect/lib/clang/18/include 
-nostdsysteminc -fsyntax-only -fopenmp -fopenmp-version=45 
-verify=expected,omp45 
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c
 -Wuninitialized
1.  
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c:359:1:
 at annotation token
2.  
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c:350:33:
 parsing function body 'test_safelen_simdlen'
3.  
/home/nathan/cbl/src/llvm-project/clang/test/OpenMP/distribute_parallel_for_simd_misc_messages.c:350:33:
 in compound statement ('{}')
 #0 0x564839563fb8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x7787fb8)
 #1 0x564839561b7e llvm::sys::RunSignalHandlers() 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x7785b7e)
 #2 0x564839564668 SignalHandler(int) Signals.cpp:0:0
 #3 0x7f1976577710 (/usr/lib/libc.so.6+0x3e710)
 #4 0x56483bd312a1 clang::Sema::ActOnFields(clang::Scope*, 
clang::SourceLocation, clang::Decl*, llvm::ArrayRef, 
clang::SourceLocation, clang::SourceLocation, clang::ParsedAttributesView 
const&) (/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9f552a1)
 #5 0x56483c304887 clang::Sema::ActOnCapturedRegionError() 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0xa528887)
 #6 0x56483c1cdb48 
clang::Sema::ActOnOpenMPRegionEnd(clang::ActionResult, 
llvm::ArrayRef) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0xa3f1b48)
 #7 0x56483bac79ec 
clang::Parser::ParseOpenMPDeclarativeOrExecutableDirective(clang::Parser::ParsedStmtContext,
 bool) (/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9ceb9ec)
 #8 0x56483ba955ba 
clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, 
clang::ParsedAttributes&, clang::ParsedAttributes&) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cb95ba)
 #9 0x56483ba93972 
clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cb7972)
#10 0x56483ba93810 clang::Parser::ParseStatement(clang::SourceLocation*, 
clang::Parser::ParsedStmtContext) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cb7810)
#11 0x56483bac7999 
clang::Parser::ParseOpenMPDeclarativeOrExecutableDirective(clang::Parser::ParsedStmtContext,
 bool) (/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9ceb999)
#12 0x56483ba955ba 
clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, 
clang::ParsedAttributes&, clang::ParsedAttributes&) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cb95ba)
#13 0x56483ba93972 
clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cb7972)
#14 0x56483ba9dbc1 clang::Parser::ParseCompoundStatementBody(bool) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cc1bc1)
#15 0x56483ba9ec62 clang::Parser::ParseFunctionStatementBody(clang::Decl*, 
clang::Parser::ParseScope&) 
(/mnt/nvme/tmp/build/llvm-bisect/bin/clang+0x9cc2c62)
#16 0x56483b9f2ded 
clang::Parser::ParseFunctionDefinition(clang

[clang] Revert counted_by attribute feature (PR #75857)

2023-12-18 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

> @nathanchance can you test this please to verify it's unbreaking the linux 
> kernel builds?

A quick initial test shows this resolves the two cases that I found in 
https://github.com/llvm/llvm-project/issues/73168. I can do a fuller set of 
builds if necessary but since this is just backing out of 
`__attribute__((__counted_by__(...)))` altogether, I don't expect there to be 
any other issues as a result of this change (other than just uncovering other 
unrelated breakages from not having consistent ToT LLVM builds for almost a 
month in CI).

https://github.com/llvm/llvm-project/pull/75857
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libcxx] [compiler-rt] [flang] [libc] [llvm] [clang] [Clang] Use correct base expression for counted_by field (#73168) (PR #73465)

2023-11-28 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

For what it's worth, this resolves both the issues that I reported at 
https://github.com/llvm/llvm-project/issues/73168, at least when building the 
full two files that I noticed had a problem before. I can do more builds later 
if necessary.

https://github.com/llvm/llvm-project/pull/73465
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)

2024-03-20 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

I see a crash from an unreachable statement while building the Linux kernel 
with debug info enabled after this change. A trivial reproducer from `cvise`:

```c
struct {
  int num_counters;
  long value[] __attribute__((__counted_by__(num_counters)));
} agent_send_response_port_num;
```

```
$ clang -g -c -o /dev/null agent.i
type should have been unwrapped!
UNREACHABLE executed at 
/mnt/nvme/tmp/cvise.DVoC71ctTu/src/clang/lib/CodeGen/CGDebugInfo.cpp:3681!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: clang -g -c -o /dev/null agent.i
1.   parser at end of file
 #0 0x564a42c9f028 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x534c028)
 #1 0x564a42c9cc6e llvm::sys::RunSignalHandlers() 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5349c6e)
 #2 0x564a42c1f526 CrashRecoverySignalHandler(int) 
CrashRecoveryContext.cpp:0:0
 #3 0x7f91646c5770 (/usr/lib/libc.so.6+0x3c770)
 #4 0x7f916471632c (/usr/lib/libc.so.6+0x8d32c)
 #5 0x7f91646c56c8 raise (/usr/lib/libc.so.6+0x3c6c8)
 #6 0x7f91646ad4b8 abort (/usr/lib/libc.so.6+0x244b8)
 #7 0x564a42c2522f 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x52d222f)
 #8 0x564a42f3ccf1 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e9cf1)
 #9 0x564a42f2aeab 
clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55d7eab)
#10 0x564a42f31090 
clang::CodeGen::CGDebugInfo::createFieldType(llvm::StringRef, clang::QualType, 
clang::SourceLocation, clang::AccessSpecifier, unsigned long, unsigned int, 
llvm::DIFile*, llvm::DIScope*, clang::RecordDecl const*, 
llvm::MDTupleTypedArrayWrapper) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55de090)
#11 0x564a42f31dd2 
clang::CodeGen::CGDebugInfo::CollectRecordNormalField(clang::FieldDecl const*, 
unsigned long, llvm::DIFile*, llvm::SmallVectorImpl&, 
llvm::DIType*, clang::RecordDecl const*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55dedd2)
#12 0x564a42f322f8 
clang::CodeGen::CGDebugInfo::CollectRecordFields(clang::RecordDecl const*, 
llvm::DIFile*, llvm::SmallVectorImpl&, llvm::DICompositeType*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55df2f8)
#13 0x564a42f3765d 
clang::CodeGen::CGDebugInfo::CreateTypeDefinition(clang::RecordType const*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e465d)
#14 0x564a42f37b3d 
clang::CodeGen::CGDebugInfo::CreateType(clang::RecordType const*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e4b3d)
#15 0x564a42f3ca03 
clang::CodeGen::CGDebugInfo::CreateTypeNode(clang::QualType, llvm::DIFile*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e9a03)
#16 0x564a42f2aeab 
clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55d7eab)
#17 0x564a42f475b4 
clang::CodeGen::CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable*, 
clang::VarDecl const*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55f45b4)
#18 0x564a42ea15fd 
clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, 
bool) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x554e5fd)
#19 0x564a42ea2f30 
clang::CodeGen::CodeGenModule::EmitTentativeDefinition(clang::VarDecl const*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x554ff30)
#20 0x564a44895bd8 clang::Sema::ActOnEndOfTranslationUnit() 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6f42bd8)
#21 0x564a4474bf8e 
clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, 
clang::Sema::ModuleImportState&) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6df8f8e)
#22 0x564a447468be clang::ParseAST(clang::Sema&, bool, bool) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6df38be)
#23 0x564a438624af clang::FrontendAction::Execute() 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5f0f4af)
#24 0x564a437d6efd 
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5e83efd)
#25 0x564a43929a58 
clang::ExecuteCompilerInvocation(clang::CompilerInstance*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5fd6a58)
#26 0x564a40e6688f cc1_main(llvm::ArrayRef, char const*, 
void*) 
(/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x351388f)
#27 

[clang] Unwrap CountAttributed for debug info (PR #86017)

2024-03-20 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

Thanks, this resolves my reported issue!

https://github.com/llvm/llvm-project/pull/86017
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Split -Wcast-function-type into a separate group (PR #86131)

2024-03-21 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This seems reasonable to me. From the perspective of the Linux kernel, this 
seems like it should be a no-op, as we enable `-Wcast-function-type` and 
disable `-Wcast-function-type-strict` (under a normal build, it is on with 
`W=1`) explicitly:

```
$ rg 'cast-function-type(-strict)?\)' scripts/Makefile.extrawarn
57:KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
134:KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
```

https://github.com/llvm/llvm-project/pull/86131
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix missing warning when comparing mismatched enums in … (PR #81418)

2024-02-27 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

For what it's worth, this change adds several instances of 
`-Wenum-enum-conversion` for the Linux kernel: 
https://github.com/ClangBuiltLinux/linux/issues/2002. I assume this is 
intentional given the nature of the change as a whole but neither the tests nor 
the release notes seem to really reflect that. In case it is a bug that this 
change affected that, consider this a report :)

https://github.com/llvm/llvm-project/pull/81418
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-30 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

> But due to I can't reproduce the failure, @nathanchance would you like to 
> make this? I don't want to make something that I can't test.

I don't mind submitting something if I have some guidance around how this 
should be handled (I am a little lost in this thread at the moment). 
Alternatively, I am able to reproduce this with:

(`/usr/bin/clang -print-target-triple` is `x86_64-pc-linux-gnu`, if you'd 
rather use it directly)

```sh
$ cmake \
-B build \
-G Ninja \
-S llvm \
-Wno-dev \
--log-level=NOTICE \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DLLVM_DEFAULT_TARGET_TRIPLE=$(/usr/bin/clang -print-target-triple) \
-DLLVM_ENABLE_PROJECTS=clang \
-DLLVM_USE_LINKER=lld

$ ninja -C build check-clang
...
error: PCH file was compiled for the target 'x86_64-pc-linux-gnu' but the 
current translation unit is being compiled for target 'x86_64-unknown-linux-gnu'
error: module file 
.../tools/clang/test/Interpreter/Output/cxx20-modules.cppm.tmp/mod.pcm cannot 
be loaded due to a configuration mismatch with the current compilation 
[-Wmodule-file-config-mismatch]
error: Parsing failed.
...
```

so it should be easy to test and verify the fix.

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-30 Thread Nathan Chancellor via cfe-commits


@@ -0,0 +1,31 @@
+// UNSUPPORTED: system-aix
+//
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang -std=c++20 %t/mod.cppm --precompile \
+// RUN: -o %t/mod.pcm
+// RUN: %clang %t/mod.pcm -c -o %t/mod.o
+// RUN: %clang -shared %t/mod.o -o %t/libmod.so
+//
+// RUN: cat %t/import.cpp | env LD_LIBRARY_PATH=%t:$LD_LIBRARY_PATH \
+// RUN: clang-repl -Xcc=-std=c++20 -Xcc=-fmodule-file=M=%t/mod.pcm \
+// RUN: | FileCheck %t/import.cpp

nathanchance wrote:

```diff
diff --git a/clang/test/Interpreter/cxx20-modules.cppm 
b/clang/test/Interpreter/cxx20-modules.cppm
index 2c6eba525519..cd2b04fdc547 100644
--- a/clang/test/Interpreter/cxx20-modules.cppm
+++ b/clang/test/Interpreter/cxx20-modules.cppm
@@ -6,13 +6,13 @@
 // RUN: split-file %s %t
 //
 // RUN: %clang -std=c++20 %t/mod.cppm --precompile \
-// RUN: -o %t/mod.pcm
-// RUN: %clang %t/mod.pcm -c -o %t/mod.o
-// RUN: %clang -shared %t/mod.o -o %t/libmod.so
+// RUN: -o %t/mod.pcm --target=x86_64-linux-gnu
+// RUN: %clang %t/mod.pcm -c -o %t/mod.o --target=x86_64-linux-gnu
+// RUN: %clang -shared %t/mod.o -o %t/libmod.so --target=x86_64-linux-gnu
 //
 // RUN: cat %t/import.cpp | env LD_LIBRARY_PATH=%t:$LD_LIBRARY_PATH \
 // RUN: clang-repl -Xcc=-std=c++20 -Xcc=-fmodule-file=M=%t/mod.pcm \
-// RUN: | FileCheck %t/import.cpp
+// RUN: -Xcc=--target=x86_64-linux-gnu | FileCheck %t/import.cpp
 
 //--- mod.cppm
 export module M;
```

appears to work for me.

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-30 Thread Nathan Chancellor via cfe-commits


@@ -0,0 +1,31 @@
+// UNSUPPORTED: system-aix
+//
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang -std=c++20 %t/mod.cppm --precompile \
+// RUN: -o %t/mod.pcm
+// RUN: %clang %t/mod.pcm -c -o %t/mod.o
+// RUN: %clang -shared %t/mod.o -o %t/libmod.so
+//
+// RUN: cat %t/import.cpp | env LD_LIBRARY_PATH=%t:$LD_LIBRARY_PATH \
+// RUN: clang-repl -Xcc=-std=c++20 -Xcc=-fmodule-file=M=%t/mod.pcm \
+// RUN: | FileCheck %t/import.cpp

nathanchance wrote:

You can land it, thanks for continuing to chase this!

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-24 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

For what it's worth, this test appears to fail when 
`LLVM_DEFAULT_TARGET_TRIPLE` is changed (my script sets it to the output of my 
Linux distribution's `clang -print-target-triple`)

```
error: PCH file was compiled for the target 'x86_64-pc-linux-gnu' but the 
current translation unit is being compiled for target 'x86_64-unknown-linux-gnu'
error: module file 
.../tools/clang/test/Interpreter/Output/cxx20-modules.cppm.tmp/mod.pcm cannot 
be loaded due to a configuration mismatch with the current compilation 
[-Wmodule-file-config-mismatch]
error: Parsing failed.
```

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-25 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This is out of my wheelhouse to debug to be honest but it seems to me that 
`clang-repl` does not respect `LLVM_DEFAULT_TARGET_TRIPLE` somehow? This test 
does the same thing as other tests to build modules but this appears to be the 
only one that uses `clang-repl` to consume the `.pcm`.

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Support C++20 Modules in clang-repl (PR #79261)

2024-01-26 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

@vgvassilev Yes, it appears so, I tried one of the examples from [the 
documentation](https://clang.llvm.org/docs/ClangRepl.html) since I have no 
prior experience with `clang-repl`.

```
$ clang-repl --version
LLVM (http://llvm.org/):
  LLVM version 19.0.0git
  Optimized build with assertions.

$ clang-repl
clang-repl> #include 
clang-repl> int sum(int a, int b){ return a+b; };
clang-repl> int c = sum(9,10);
clang-repl> std::cout << c << std::endl;
19
clang-repl> ^D
```

https://github.com/llvm/llvm-project/pull/79261
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 9ed4a94 - [clang] Expose unreachable fallthrough annotation warning

2021-08-16 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2021-08-16T17:14:55-07:00
New Revision: 9ed4a94d6451046a51ef393cd62f00710820a7e8

URL: 
https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
DIFF: 
https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8.diff

LOG: [clang] Expose unreachable fallthrough annotation warning

The Linux kernel has a macro called IS_ENABLED(), which evaluates to a
constant 1 or 0 based on Kconfig selections, allowing C code to be
unconditionally enabled or disabled at build time. For example:

int foo(struct *a, int b) {
switch (b) {
case 1:
if (a->flag || !IS_ENABLED(CONFIG_64BIT))
return 1;
__attribute__((fallthrough));
case 2:
return 2;
default:
return 3;
}
}

There is an unreachable warning about the fallthrough annotation in the
first case because !IS_ENABLED(CONFIG_64BIT) can be evaluated to 1,
which looks like

return 1;
__attribute__((fallthrough));

to clang.

This type of warning is pointless for the Linux kernel because it does
this trick all over the place due to the sheer number of configuration
options that it has.

Add -Wunreachable-code-fallthrough, enabled under -Wunreachable-code, so
that projects that want to warn on unreachable code get this warning but
projects that do not care about unreachable code can still use
-Wimplicit-fallthrough without having to make changes to their code
base.

Fixes PR51094.

Reviewed By: aaron.ballman, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D107933

Added: 


Modified: 
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/AnalysisBasedWarnings.cpp
clang/test/SemaCXX/P30636.cpp
clang/test/SemaCXX/switch-implicit-fallthrough.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 30dadd9731c15..17b5f419ef58c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -821,8 +821,10 @@ def ReservedIdentifier : DiagGroup<"reserved-identifier",
 //  under separate flags.
 //
 def UnreachableCodeLoopIncrement : 
DiagGroup<"unreachable-code-loop-increment">;
+def UnreachableCodeFallthrough : DiagGroup<"unreachable-code-fallthrough">;
 def UnreachableCode : DiagGroup<"unreachable-code",
-[UnreachableCodeLoopIncrement]>;
+[UnreachableCodeLoopIncrement,
+ UnreachableCodeFallthrough]>;
 def UnreachableCodeBreak : DiagGroup<"unreachable-code-break">;
 def UnreachableCodeReturn : DiagGroup<"unreachable-code-return">;
 def UnreachableCodeAggressive : DiagGroup<"unreachable-code-aggressive",

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9eaa696d99913..41152212c0d12 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -682,6 +682,9 @@ def warn_unreachable_return : Warning<
 def warn_unreachable_loop_increment : Warning<
   "loop will run at most once (loop increment never executed)">,
   InGroup, DefaultIgnore;
+def warn_unreachable_fallthrough_attr : Warning<
+  "fallthrough annotation in unreachable code">,
+  InGroup, DefaultIgnore;
 def note_unreachable_silence : Note<
   "silence by adding parentheses to mark code as explicitly dead">;
 
@@ -9578,9 +9581,6 @@ def err_fallthrough_attr_outside_switch : Error<
   "fallthrough annotation is outside switch statement">;
 def err_fallthrough_attr_invalid_placement : Error<
   "fallthrough annotation does not directly precede switch label">;
-def warn_fallthrough_attr_unreachable : Warning<
-  "fallthrough annotation in unreachable code">,
-  InGroup, DefaultIgnore;
 
 def warn_unreachable_default : Warning<
   "default label in switch which covers all enumeration values">,

diff  --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index aa2602c8d9256..99ce143d3559d 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1125,7 +1125,7 @@ namespace {
 // unreachable in all instantiations of the template.
 if (!IsTemplateInstantiation)
   S.Diag(AS->getBeginLoc(),
- diag::warn_fallthrough_attr_unreachable);
+ diag::warn_unreachable_fallthrough_attr);
 markFallthroughVisited(AS);
 ++AnnotatedCnt;
 break;

diff  --git a/clang/test/SemaCXX/P30636.cpp b/clang/test/SemaCXX/P30636.cpp
index 2e2affb0cfdea..1d5400d3ba0ed 100644
--- a/clang/test/SemaCXX/P30636.cpp
+++ b/clang/test/SemaCXX/P30636.cpp
@@ -1,

[clang] 17f4f26 - Revert "Reapply [IR] Mark and constant expressions as undesirable"

2023-07-21 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2023-07-21T15:57:03-07:00
New Revision: 17f4f262fc5c4361cf43e91f2137ff7b2dcadc62

URL: 
https://github.com/llvm/llvm-project/commit/17f4f262fc5c4361cf43e91f2137ff7b2dcadc62
DIFF: 
https://github.com/llvm/llvm-project/commit/17f4f262fc5c4361cf43e91f2137ff7b2dcadc62.diff

LOG: Revert "Reapply [IR] Mark and constant expressions as undesirable"

This reverts commit 086ee99564afbb11449c08ea2e094f7f49fadde5.

This patch causes an infinite loop when building arch/mips/mm/c-r4k.c in
the Linux kernel. See the comment in Phabricator for a reduced
reproducer: https://reviews.llvm.org/rG086ee99564afbb11449c08ea2e094f7f49fadde5

Added: 


Modified: 
clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
llvm/lib/IR/ConstantFold.cpp
llvm/lib/IR/Constants.cpp
llvm/test/CodeGen/Hexagon/atomic-opaque-basic.ll
llvm/test/Transforms/InstCombine/and-xor-or.ll
llvm/test/Transforms/InstCombine/bswap-fold.ll
llvm/test/Transforms/InstSimplify/ConstProp/constant-expr.ll
llvm/test/Transforms/InstSimplify/compare.ll

Removed: 




diff  --git a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c 
b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
index 0e0a9b157464a6..551ccc4810bcd3 100644
--- a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
+++ b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
@@ -303,8 +303,7 @@ char *one_zero(void) {
 char *one_one_OK(void) {
   // CHECK:   define{{.*}} ptr @one_one_OK()
   // CHECK-NEXT:  [[ENTRY:.*]]:
-  // CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr 
inttoptr (i64 1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint 
(ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), 
i64 1), i64 1), i64 0), !nosanitize
-  // CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], 
label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
+  // CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr 
(i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr 
getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 
1), i64 1), i64 0)), label %[[CONT:.*]], label 
%[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
   // CHECK-SANITIZE-CPP-NEXT:   br i1 xor (i1 icmp eq (ptr inttoptr 
(i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr 
getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 
1), i64 1), i64 0)), label %[[CONT:.*]], label 
%[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
   // CHECK-SANITIZE:  [[HANDLER_POINTER_OVERFLOW]]:
   // CHECK-SANITIZE-NORECOVER-NEXT: call void 
@__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1100]], i64 1, i64 add (i64 
sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), 
i64 1) to i64), i64 1), i64 1))
@@ -322,8 +321,7 @@ char *one_one_OK(void) {
 char *one_allones_BAD(void) {
   // CHECK:   define{{.*}} ptr @one_allones_BAD()
   // CHECK-NEXT:  [[ENTRY:.*]]:
-  // CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr 
inttoptr (i64 1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint 
(ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), 
i64 1), i64 1), i64 0), !nosanitize
-  // CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], 
label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
+  // CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr 
(i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr 
getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 
1), i64 1), i64 0)), label %[[CONT:.*]], label 
%[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
   // CHECK-SANITIZE-CPP-NEXT:   br i1 xor (i1 icmp eq (ptr inttoptr 
(i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr 
getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 
1), i64 1), i64 0)), label %[[CONT:.*]], label 
%[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
   // CHECK-SANITIZE:  [[HANDLER_POINTER_OVERFLOW]]:
   // CHECK-SANITIZE-NORECOVER-NEXT: call void 
@__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1200]], i64 1, i64 add (i64 
sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), 
i64 -1) to i64), i64 1), i64 1))
@@ -392,8 +390,7 @@ char *allones_zero_OK(void) {
 char *allones_one_BAD(void) {
   // CHECK: define{{.*}} ptr @allones_one_BAD()
   // CHECK-NEXT: [[ENTRY:.*]]:
-  // CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr 
inttoptr (i64 -1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint 
(ptr getelementptr inbounds (i8, ptr intt

[clang] a22d385 - [Sema] Do not emit -Wmissing-variable-declarations for register variables

2023-08-08 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2023-08-08T13:41:21-07:00
New Revision: a22d385f9656c95f5ce4155ea705aab6f8ef6d82

URL: 
https://github.com/llvm/llvm-project/commit/a22d385f9656c95f5ce4155ea705aab6f8ef6d82
DIFF: 
https://github.com/llvm/llvm-project/commit/a22d385f9656c95f5ce4155ea705aab6f8ef6d82.diff

LOG: [Sema] Do not emit -Wmissing-variable-declarations for register variables

When building the Linux kernel with -Wmissing-variable-declarations,
there are several instances of warnings around variables declared with
register storage:

  arch/x86/include/asm/asm.h:208:24: warning: no previous extern declaration 
for non-static variable 'current_stack_pointer' 
[-Wmissing-variable-declarations]
  register unsigned long current_stack_pointer asm(_ASM_SP);
 ^
  arch/x86/include/asm/asm.h:208:10: note: declare 'static' if the variable is 
not intended to be used outside of this translation unit
  register unsigned long current_stack_pointer asm(_ASM_SP);
   ^
  1 warning generated.

The suggestion is invalid, as the variable cannot have both static and
register storage. Do not emit -Wmissing-variable-declarations for
register variables.

Closes: https://github.com/llvm/llvm-project/issues/64509
Link: https://lore.kernel.org/202308081050.szew4cq5-...@intel.com/
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110947

Reviewed By: aaron.ballman, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D157435

Added: 
clang/test/Sema/warn-missing-variable-declarations-register.c

Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaDecl.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d3757c833ef83a..92abb65546caa3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -123,6 +123,8 @@ Improvements to Clang's diagnostics
   template-specialization function calls.
 - Clang contexpr evaluator now displays notes as well as an error when a 
constructor
   of a base class is not called in the constructor of its derived class.
+- Clang no longer emits ``-Wmissing-variable-declarations`` for variables 
declared
+  with the ``register`` storage class.
 
 Bug Fixes in This Version
 -

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 390d09547f2a58..d250b4382753bb 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14144,6 +14144,7 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl 
*var) {
   var->getDeclContext()->getRedeclContext()->isFileContext() &&
   var->isExternallyVisible() && var->hasLinkage() &&
   !var->isInline() && !var->getDescribedVarTemplate() &&
+  var->getStorageClass() != SC_Register &&
   !isa(var) &&
   !isTemplateInstantiation(var->getTemplateSpecializationKind()) &&
   !getDiagnostics().isIgnored(diag::warn_missing_variable_declarations,

diff  --git a/clang/test/Sema/warn-missing-variable-declarations-register.c 
b/clang/test/Sema/warn-missing-variable-declarations-register.c
new file mode 100644
index 00..d26a0df12e23dc
--- /dev/null
+++ b/clang/test/Sema/warn-missing-variable-declarations-register.c
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -Wmissing-variable-declarations 
-fsyntax-only -verify %s
+// expected-no-diagnostics
+
+register unsigned long current_stack_pointer asm("rsp");



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-07 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance review_requested 
https://github.com/llvm/llvm-project/pull/65657
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-07 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance created 
https://github.com/llvm/llvm-project/pull/65657:

When building the LoongArch Linux kernel without `CONFIG_DYNAMIC_FTRACE`, the 
build fails to link because the mcount symbol is `mcount`, not `_mcount` like 
GCC generates and the kernel expects:

```
ld.lld: error: undefined symbol: mcount
>>> referenced by version.c
>>>   init/version.o:(early_hostname) in archive vmlinux.a
>>> referenced by do_mounts.c
>>>   init/do_mounts.o:(rootfs_init_fs_context) in archive vmlinux.a
>>> referenced by main.c
>>>   init/main.o:(__traceiter_initcall_level) in archive vmlinux.a
>>> referenced 97011 more times
>>> did you mean: _mcount
>>> defined in: vmlinux.a(arch/loongarch/kernel/mcount.o)
```

Set `MCountName` in `LoongArchTargetInfo` to `_mcount`, which resolves the 
build failure.


>From a359527254c6b6be9f988a4fc65a7b6d307f4514 Mon Sep 17 00:00:00 2001
From: Nathan Chancellor 
Date: Thu, 7 Sep 2023 11:59:05 -0700
Subject: [PATCH] [Clang][LoongArch] Generate _mcount instead of mcount

When building the LoongArch Linux kernel without CONFIG_DYNAMIC_FTRACE,
the build fails to link because the mcount symbol is "mcount", not
"_mcount" like GCC generates and the kernel expects:

  ld.lld: error: undefined symbol: mcount
  >>> referenced by version.c
  >>>   init/version.o:(early_hostname) in archive vmlinux.a
  >>> referenced by do_mounts.c
  >>>   init/do_mounts.o:(rootfs_init_fs_context) in archive 
vmlinux.a
  >>> referenced by main.c
  >>>   init/main.o:(__traceiter_initcall_level) in archive 
vmlinux.a
  >>> referenced 97011 more times
  >>> did you mean: _mcount
  >>> defined in: vmlinux.a(arch/loongarch/kernel/mcount.o)

Set MCountName in LoongArchTargetInfo to "_mcount", which resolves the build
failure.
---
 clang/lib/Basic/Targets/LoongArch.h | 1 +
 clang/test/CodeGen/mcount.c | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/clang/lib/Basic/Targets/LoongArch.h 
b/clang/lib/Basic/Targets/LoongArch.h
index 34143f462a24722..ba7fb78ab94cd23 100644
--- a/clang/lib/Basic/Targets/LoongArch.h
+++ b/clang/lib/Basic/Targets/LoongArch.h
@@ -36,6 +36,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public 
TargetInfo {
 LongDoubleWidth = 128;
 LongDoubleAlign = 128;
 LongDoubleFormat = &llvm::APFloat::IEEEquad();
+MCountName = "_mcount";
 SuitableAlign = 128;
 WCharType = SignedInt;
 WIntType = UnsignedInt;
diff --git a/clang/test/CodeGen/mcount.c b/clang/test/CodeGen/mcount.c
index 8f994ab4e75443a..bdd609c1dfc5826 100644
--- a/clang/test/CodeGen/mcount.c
+++ b/clang/test/CodeGen/mcount.c
@@ -7,6 +7,8 @@
 // RUN: %clang_cc1 -pg -triple x86_64-netbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple arm-netbsd-eabi -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple aarch64-netbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple loongarch32 -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple loongarch64 -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple mips-netbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple mips-unknown-gnu-linux -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple mipsel-unknown-gnu-linux -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-07 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance review_requested 
https://github.com/llvm/llvm-project/pull/65657
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-07 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance review_requested 
https://github.com/llvm/llvm-project/pull/65657
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-07 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

cc @xen0n

https://github.com/llvm/llvm-project/pull/65657
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][LoongArch] Generate _mcount instead of mcount (PR #65657)

2023-09-08 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance closed 
https://github.com/llvm/llvm-project/pull/65657
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 877210f - [Sema] Do not emit -Wunused-variable for variables declared with cleanup attribute

2023-06-05 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2023-06-05T10:54:47-07:00
New Revision: 877210faa447f4cc7db87812f8ed80e398fedd61

URL: 
https://github.com/llvm/llvm-project/commit/877210faa447f4cc7db87812f8ed80e398fedd61
DIFF: 
https://github.com/llvm/llvm-project/commit/877210faa447f4cc7db87812f8ed80e398fedd61.diff

LOG: [Sema] Do not emit -Wunused-variable for variables declared with cleanup 
attribute

A variable declared with __attribute__((cleanup)) cannot be unused, as
its address is passed to the clean up function. Do not emit
-Wunused-variable for variables declared with the cleanup attribute,
which matches GCC's behavior: https://godbolt.org/z/dz5YfTsan

Reviewed By: erichkeane, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D152180

Added: 


Modified: 
clang/lib/Sema/SemaDecl.cpp
clang/test/Sema/warn-unused-variables.c

Removed: 




diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b8aba816283d6..12fd378fb170c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1992,7 +1992,8 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
 return false;
   }
 
-  if (D->hasAttr() || D->hasAttr())
+  if (D->hasAttr() || D->hasAttr() ||
+  D->hasAttr())
 return false;
 
   if (isa(D))

diff  --git a/clang/test/Sema/warn-unused-variables.c 
b/clang/test/Sema/warn-unused-variables.c
index d482c353f0ad1..26b18720849f9 100644
--- a/clang/test/Sema/warn-unused-variables.c
+++ b/clang/test/Sema/warn-unused-variables.c
@@ -30,3 +30,8 @@ int f3(void) {
   (void)(^() { int X = 4; }); // expected-warning{{unused}}
   (void)(^() { int X = 4; return Y + X; }); // expected-error {{use of 
undeclared identifier 'Y'}}
 }
+
+void c1(int *);
+void f4(void) {
+  int __attribute__((cleanup(c1))) X1 = 4;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] deecf89 - [Clang] Add release note for 877210faa447

2023-06-05 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2023-06-05T12:14:51-07:00
New Revision: deecf89a1361cf3407f0fe32e7085127ec0865dc

URL: 
https://github.com/llvm/llvm-project/commit/deecf89a1361cf3407f0fe32e7085127ec0865dc
DIFF: 
https://github.com/llvm/llvm-project/commit/deecf89a1361cf3407f0fe32e7085127ec0865dc.diff

LOG: [Clang] Add release note for 877210faa447

Reviewed By: erichkeane, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D152186

Added: 


Modified: 
clang/docs/ReleaseNotes.rst

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f1ac1cb79fe23..8db6de896a121 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -330,6 +330,8 @@ Improvements to Clang's diagnostics
   ``-fno-diagnostics-show-line-numbers``. At the same time, the maximum
   number of code lines it prints has been increased from 1 to 16. This
   can be controlled using ``-fcaret-diagnostics-max-lines=``.
+- Clang no longer emits ``-Wunused-variable`` warnings for variables declared
+  with ``__attribute__((cleanup(...)))`` to match GCC's behavior.
 
 Bug Fixes in This Version
 -



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64] Add soft-float ABI (PR #74460)

2024-02-16 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

For what it's worth, this breaks building the Linux kernel for `ARCH=arm64` 
pretty badly, with errors in several drivers:

```
$ make -skj"$(nrpoc)" ARCH=arm64 LLVM=1 mrproper defconfig all
...
drivers/clk/qcom/gcc-ipq6018.c:896:2: error: expression requires 'double' type 
support, but ABI 'aapcs' does not support it
  896 | F(53300, P_GPLL0, 1.5, 0, 0),
  | ^
drivers/clk/qcom/clk-rcg.h:10:41: note: expanded from macro 'F'
   10 | #define F(f, s, h, m, n) { (f), (s), (2 * (h) - 1), (m), (n) }
  | ^
...
In file included from drivers/gpu/drm/msm/adreno/a2xx_gpu.c:4:
In file included from drivers/gpu/drm/msm/adreno/a2xx_gpu.h:13:
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1849:24: error: 'A2XX_PA_CL_VPORT_XSCALE' 
requires 'float' type support, but ABI 'aapcs' does not support it
 1849 | static inline uint32_t A2XX_PA_CL_VPORT_XSCALE(float val)
  |^
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1849:24: note: 'A2XX_PA_CL_VPORT_XSCALE' 
defined here
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1857:24: error: 
'A2XX_PA_CL_VPORT_XOFFSET' requires 'float' type support, but ABI 'aapcs' does 
not support it
 1857 | static inline uint32_t A2XX_PA_CL_VPORT_XOFFSET(float val)
  |^
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1857:24: note: 'A2XX_PA_CL_VPORT_XOFFSET' 
defined here
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1865:24: error: 'A2XX_PA_CL_VPORT_YSCALE' 
requires 'float' type support, but ABI 'aapcs' does not support it
 1865 | static inline uint32_t A2XX_PA_CL_VPORT_YSCALE(float val)
  |^
drivers/gpu/drm/msm/adreno/a2xx.xml.h:1865:24: note: 'A2XX_PA_CL_VPORT_YSCALE' 
defined here
...
```

I suspect this may be related to the kernel's use of `-mgeneral-regs-only`? Up 
until this point though, there have been no reported issues with code 
generation or runtime time impact with `clang`-built kernels.

https://github.com/llvm/llvm-project/pull/74460
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Revert "[AArch64] Add soft-float ABI (#74460)" (PR #82032)

2024-02-16 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This does not appear to drop `clang/test/Driver/aarch64-soft-float-abi.c` which 
was added by that change, do the tests pass with that test still present? I am 
guessing there was a conflict because of the forward fix in 
5b8e7ed787f6e537876c4fdafd070eba9681f343.

Regardless, I approve of a revert if there is no quick forward fix, our 
continuous integration is quite red because of this.

https://github.com/llvm/llvm-project/pull/82032
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Remove support for 3DNow!, both intrinsics and builtins. (PR #96246)

2024-07-17 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

I am seeing a lot of

```
'-3dnow' is not a recognized feature for this target (ignoring feature)
'-3dnowa' is not a recognized feature for this target (ignoring feature)
```

while building the Linux kernel (which has `-mno-3dnow` in 
`arch/x86/Makefile`), is this intentional behavior? I don't have a standalone 
reproducer yet (I am dealing with a lot of other breakage at the moment) but I 
wanted to inquire on whether or not that should be happening. It appears that 
`-mno-3dnow` is still allowed, so we could not use our `cc-option` macro, we 
would have to remove it only when LLVM is at least 19.0.0.

https://github.com/llvm/llvm-project/pull/96246
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Loop over FieldDecls instead of all Decls (PR #99574)

2024-07-21 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

I see a crash in `drivers/thermal/thermal_core.c` in Linux kernels without 
commit 
[daeeb032f42d](https://git.kernel.org/linus/daeeb032f42d066a49e07b7f6effc9f51b7a5479)
 ("thermal: core: Move threshold out of struct thermal_trip") after this 
change. `cvise` spits out:

```c
struct swait_queue_head {};
struct completion {
  int done;
  struct swait_queue_head wait;
};
struct {
  struct completion removal;
  int num_trips;
  int trips[] __attribute__((__counted_by__(num_trips)));
} *thermal_zone_device_register_with_trips_tz;
int thermal_zone_device_register_with_trips() {
  if (thermal_zone_device_register_with_trips_tz)
goto free_tz;
  if (0)
goto free_tzp;
  {
long __p_size_field = __builtin_dynamic_object_size(
thermal_zone_device_register_with_trips_tz, 1);
_Bool __ret_do_once = __p_size_field, __ret_cond = __ret_do_once;
static _Bool __already_done;
if (__builtin_expect(__ret_cond && __already_done, 0))
  ;
  }
free_tzp:
free_tz:
  return 0;
}
```

```
$ clang -c -o /dev/null thermal_core.i
clang: 
/home/nathan/tmp/cvise.YrEYUS5WBZ/src/clang/lib/CodeGen/CGRecordLayout.h:200: 
unsigned int clang::CodeGen::CGRecordLayout::getLLVMFieldNo(const FieldDecl *) 
const: Assertion `FieldInfo.count(FD) && "Invalid field for record!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: clang -c -o /dev/null thermal_core.i
1.   parser at end of file
2.  thermal_core.i:11:5: LLVM IR generation of declaration 
'thermal_zone_device_register_with_trips'
3.  thermal_core.i:11:5: Generating code for declaration 
'thermal_zone_device_register_with_trips'
4.  thermal_core.i:16:3: LLVM IR generation of compound statement ('{}')
 #0 0x5585369d0446 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x55fc446)
 #1 0x5585369cdf0e llvm::sys::RunSignalHandlers() 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x55f9f0e)
 #2 0x5585369513ad CrashRecoverySignalHandler(int) 
CrashRecoveryContext.cpp:0:0
 #3 0x7f3d209bdae0 (/usr/lib/libc.so.6+0x3cae0)
 #4 0x7f3d20a15e44 (/usr/lib/libc.so.6+0x94e44)
 #5 0x7f3d209bda30 raise (/usr/lib/libc.so.6+0x3ca30)
 #6 0x7f3d209a54c3 abort (/usr/lib/libc.so.6+0x244c3)
 #7 0x7f3d209a53df (/usr/lib/libc.so.6+0x243df)
 #8 0x7f3d209b5c67 (/usr/lib/libc.so.6+0x34c67)
 #9 0x558536de6281 
clang::CodeGen::CGRecordLayout::getLLVMFieldNo(clang::FieldDecl const*) const 
CGExpr.cpp:0:0
#10 0x558536dcd526 getGEPIndicesToField(clang::CodeGen::CodeGenFunction&, 
clang::RecordDecl const*, clang::FieldDecl const*, 
llvm::SmallVector, 8u>&) 
CGExpr.cpp:0:0
#11 0x558536dcd581 getGEPIndicesToField(clang::CodeGen::CodeGenFunction&, 
clang::RecordDecl const*, clang::FieldDecl const*, 
llvm::SmallVector, 8u>&) 
CGExpr.cpp:0:0
#12 0x558536dca542 
clang::CodeGen::CodeGenFunction::EmitCountedByFieldExpr(clang::Expr const*, 
clang::FieldDecl const*, clang::FieldDecl const*) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x59f6542)
#13 0x558536fb0927 
clang::CodeGen::CodeGenFunction::emitFlexibleArrayMemberSize(clang::Expr 
const*, unsigned int, llvm::IntegerType*) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5bdc927)
#14 0x558536fafdf8 
clang::CodeGen::CodeGenFunction::emitBuiltinObjectSize(clang::Expr const*, 
unsigned int, llvm::IntegerType*, llvm::Value*, bool) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5bdbdf8)
#15 0x558536fb9cfc 
clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned 
int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5be5cfc)
#16 0x558536de827c 
clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, 
clang::CodeGen::ReturnValueSlot) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5a1427c)
#17 0x558536e024cb (anonymous 
namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) 
CGExprScalar.cpp:0:0
#18 0x558536dee27b (anonymous 
namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
#19 0x558536e0bedd (anonymous 
namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) 
CGExprScalar.cpp:0:0
#20 0x558536dee11c 
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5a1a11c)
#21 0x558536e301e4 
clang::CodeGen::CodeGenFunction::EmitScalarInit(clang::Expr const*, 
clang::ValueDecl const*, clang::CodeGen::LValue, bool) 
(/home/nathan/tmp/cvise.YrEYUS5WBZ/install/llvm-bad/bin/clang-19+0x5a5c1e4)
#22 0x558536e335d1 
clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction

[clang] be8180a - [clang][driver] Warn when '-mno-outline-atomics' is used with a non-AArch64 triple

2021-12-23 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2021-12-23T12:36:42-07:00
New Revision: be8180af5854806a343c3dd334d97ba2c4bfadfa

URL: 
https://github.com/llvm/llvm-project/commit/be8180af5854806a343c3dd334d97ba2c4bfadfa
DIFF: 
https://github.com/llvm/llvm-project/commit/be8180af5854806a343c3dd334d97ba2c4bfadfa.diff

LOG: [clang][driver] Warn when '-mno-outline-atomics' is used with a 
non-AArch64 triple

The Linux kernel has a make macro called cc-option that invokes the
compiler with an option in isolation to see if it is supported before
adding it to CFLAGS. The exit code of the compiler is used to determine
if the flag is supported and should be added to the compiler invocation.

A call to cc-option with '-mno-outline-atomics' was added to prevent
linking errors with newer GCC versions but this call succeeds with a
non-AArch64 target because there is no warning from clang with
'-mno-outline-atomics', just '-moutline-atomics'. Because the call
succeeds and adds '-mno-outline-atomics' to the compiler invocation,
there is a warning from LLVM because the 'outline-atomics target
feature is only supported by the AArch64 backend.

$ echo | clang -target x86_64 -moutline-atomics -Werror -x c -c -o /dev/null -
clang-14: error: The 'x86_64' architecture does not support -moutline-atomics; 
flag ignored [-Werror,-Woption-ignored]

$ echo $?
1

$ echo | clang -target x86_64 -mno-outline-atomics -Werror -x c -c -o /dev/null 
-
'-outline-atomics' is not a recognized feature for this target (ignoring 
feature)

$ echo $?
0

This does not match GCC's behavior, which errors when the flag is added
to a non-AArch64 target.

$ echo | gcc -moutline-atomics -x c -c -o /dev/null -
gcc: error: unrecognized command-line option ‘-moutline-atomics’; did you mean 
‘-finline-atomics’?

$ echo | gcc -mno-outline-atomics -x c -c -o /dev/null -
gcc: error: unrecognized command-line option ‘-mno-outline-atomics’; did you 
mean ‘-fno-inline-atomics’?

$ echo | aarch64-linux-gnu-gcc -moutline-atomics -x c -c -o /dev/null -

$ echo | aarch64-linux-gnu-gcc -mno-outline-atomics -x c -c -o /dev/null -

To get closer to  GCC's behavior, issue a warning when
'-mno-outline-atomics' is used without an AArch64 triple and do not add
'{-,+}outline-atomic" to the list of target features in these cases.

Link: https://github.com/ClangBuiltLinux/linux/issues/1552

Reviewed By: melver, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D116128

Added: 
clang/test/Driver/unsupported-outline-atomics.c

Modified: 
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/lib/Driver/ToolChains/Clang.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 3025e6fe3c02d..c623aeff11f05 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -568,7 +568,7 @@ def warn_drv_moutline_unsupported_opt : Warning<
   InGroup;
 
 def warn_drv_moutline_atomics_unsupported_opt : Warning<
-  "'%0' does not support '-moutline-atomics'; flag ignored">,
+  "'%0' does not support '-%1'; flag ignored">,
   InGroup;
 
 def warn_drv_darwin_sdk_invalid_settings : Warning<

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index ca62229b8153e..65347a38490ee 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7007,18 +7007,18 @@ void Clang::ConstructJob(Compilation &C, const 
JobAction &JA,
 
   if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics,
options::OPT_mno_outline_atomics)) {
-if (A->getOption().matches(options::OPT_moutline_atomics)) {
-  // Option -moutline-atomics supported for AArch64 target only.
-  if (!Triple.isAArch64()) {
-D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt)
-<< Triple.getArchName();
-  } else {
+// Option -moutline-atomics supported for AArch64 target only.
+if (!Triple.isAArch64()) {
+  D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt)
+  << Triple.getArchName() << A->getOption().getName();
+} else {
+  if (A->getOption().matches(options::OPT_moutline_atomics)) {
 CmdArgs.push_back("-target-feature");
 CmdArgs.push_back("+outline-atomics");
+  } else {
+CmdArgs.push_back("-target-feature");
+CmdArgs.push_back("-outline-atomics");
   }
-} else {
-  CmdArgs.push_back("-target-feature");
-  CmdArgs.push_back("-outline-atomics");
 }
   } else if (Triple.isAArch64() &&
  getToolChain().IsAArch64OutlineAtomicsDefault(Args)) {

diff  --git a/clang/test/Driver/unsupported-outline-atomics.c 
b/clang/test/Driver/unsupported-outline-atomics.c
new file mode 100644
index 0..2e88528c2ec60
--- /dev/null
+++ b/clang/test/Driver/unsupported

[clang] ef58ae8 - [RISCV] Fix mcount name

2021-03-24 Thread Nathan Chancellor via cfe-commits

Author: Nathan Chancellor
Date: 2021-03-24T18:11:37-07:00
New Revision: ef58ae86ba778ed7d01cd3f6bd6d08f943abab44

URL: 
https://github.com/llvm/llvm-project/commit/ef58ae86ba778ed7d01cd3f6bd6d08f943abab44
DIFF: 
https://github.com/llvm/llvm-project/commit/ef58ae86ba778ed7d01cd3f6bd6d08f943abab44.diff

LOG: [RISCV] Fix mcount name

GCC's name for this symbol is _mcount, which the Linux kernel expects in
a few different place:

  $ echo 'int main(void) { return 0; }' | riscv32-linux-gcc -c -pg -o tmp.o -x 
c -

  $ llvm-objdump -dr tmp.o | grep mcount
  000c:  R_RISCV_CALL _mcount

  $ echo 'int main(void) { return 0; }' | riscv64-linux-gcc -c -pg -o tmp.o -x 
c -

  $ llvm-objdump -dr tmp.o | grep mcount
  000c:  R_RISCV_CALL _mcount

  $ echo 'int main(void) { return 0; }' | clang -c -pg -o tmp.o 
--target=riscv32-linux-gnu -x c -

  $ llvm-objdump -dr tmp.o | grep mcount
  000a:  R_RISCV_CALL_PLT mcount

  $ echo 'int main(void) { return 0; }' | clang -c -pg -o tmp.o 
--target=riscv64-linux-gnu -x c -

  $ llvm-objdump -dr tmp.o | grep mcount
  000a:  R_RISCV_CALL_PLT mcount

Set MCountName to "_mcount" in RISCVTargetInfo then prevent it from
getting overridden in certain OSTargetInfo constructors.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D98881

Signed-off-by: Nathan Chancellor 

Added: 


Modified: 
clang/lib/Basic/Targets/OSTargets.h
clang/lib/Basic/Targets/RISCV.h
clang/test/CodeGen/mcount.c

Removed: 




diff  --git a/clang/lib/Basic/Targets/OSTargets.h 
b/clang/lib/Basic/Targets/OSTargets.h
index 539466c4f678..4de1b8d2db4f 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -261,6 +261,9 @@ class LLVM_LIBRARY_VISIBILITY FreeBSDTargetInfo : public 
OSTargetInfo {
 case llvm::Triple::arm:
   this->MCountName = "__mcount";
   break;
+case llvm::Triple::riscv32:
+case llvm::Triple::riscv64:
+  break;
 }
   }
 };
@@ -491,6 +494,9 @@ class LLVM_LIBRARY_VISIBILITY OpenBSDTargetInfo : public 
OSTargetInfo {
 case llvm::Triple::sparcv9:
   this->MCountName = "_mcount";
   break;
+case llvm::Triple::riscv32:
+case llvm::Triple::riscv64:
+  break;
 }
   }
 };

diff  --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index abae51e75a19..8df6e05ebcd5 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -59,6 +59,7 @@ class RISCVTargetInfo : public TargetInfo {
 WCharType = SignedInt;
 WIntType = UnsignedInt;
 HasRISCVVTypes = true;
+MCountName = "_mcount";
   }
 
   bool setCPU(const std::string &Name) override {

diff  --git a/clang/test/CodeGen/mcount.c b/clang/test/CodeGen/mcount.c
index 649f0b56949d..8f994ab4e754 100644
--- a/clang/test/CodeGen/mcount.c
+++ b/clang/test/CodeGen/mcount.c
@@ -12,6 +12,13 @@
 // RUN: %clang_cc1 -pg -triple mipsel-unknown-gnu-linux -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple mips64-unknown-gnu-linux -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple mips64el-unknown-gnu-linux -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv32-elf -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv64-elf -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv32-linux -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv64-linux -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv64-freebsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv64-freebsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
+// RUN: %clang_cc1 -pg -triple riscv64-openbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple powerpc-netbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple powerpc64-netbsd -emit-llvm -o - %s | FileCheck 
-check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s
 // RUN: %clang_cc1 -pg -triple powerpc64le-netbsd -emit-llvm -o - %s | 
FileCheck -check-prefixes=CHECK-DOUBLE-PREFIXED,NO-MCOUNT1 %s



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PowerPC][ISelLowering] Support -mstack-protector-guard=tls (PR #110928)

2024-10-07 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

With [my series to fix 
`arch/powerpc`](https://git.kernel.org/pub/scm/linux/kernel/git/nathan/linux.git/log/?h=b4/powerpc-fix-stackprotector-test-clang)
 for this implementation applied to Linux, `CONFIG_HAVE_STACKPROTECTOR` is 
properly set and the Linux kernel dump test module (LKDTM)’s 
`REPORT_STACK_CANARY` test works correctly for both 32-bit and 64-bit. I think 
with a PowerPC specific review, we should be ready to land this.

https://github.com/llvm/llvm-project/pull/110928
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [libcxx] [clang] Warn about memset/memcpy to NonTriviallyCopyable types (PR #111434)

2024-10-29 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

Is it expected that this warns for C code? I only see `cxx` in this change but 
I see a new instance of this warning in my Linux kernel builds:

```
drivers/net/ethernet/netronome/nfp/nfdk/rings.c:60:18: warning: first argument 
in call to '__builtin_memset' is a pointer to non-trivially copyable type 
'struct nfp_nfd3_tx_desc' [-Wnontrivial-memaccess]
   60 | memset(tx_ring->txds, 0, tx_ring->size);
  | ^
drivers/net/ethernet/netronome/nfp/nfdk/rings.c:60:18: note: explicitly cast 
the pointer to silence this warning
   60 | memset(tx_ring->txds, 0, tx_ring->size);
  | ^
  |(void*)
include/linux/fortify-string.h:512:46: note: expanded from macro 'memset'
  512 | #define memset(p, c, s) __fortify_memset_chk(p, c, s,   
\
  |  ^
include/linux/fortify-string.h:504:22: note: expanded from macro 
'__fortify_memset_chk'
  504 | __underlying_memset(p, c, __fortify_size);  
\
  | ^
1 warning generated.
```

https://elixir.bootlin.com/linux/v6.11.5/source/drivers/net/ethernet/netronome/nfp/nfdk/rings.c#L60

https://github.com/llvm/llvm-project/pull/111434
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [libcxx] [clang] Warn about memset/memcpy to NonTriviallyCopyable types (PR #111434)

2024-10-29 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

> @nathanchance could you share your preprocessed source so that I can get a 
> reproducer? Thanks!

Sure thing!

```
$ clang --target=x86_64-linux-gnu -fcf-protection -fsyntax-only 
-Wno-{address-of-packed-member,constant-logical-operand,gnu,unused-value} 
-Wnontrivial-memaccess rings.i
rings.i:95832:214: warning: first argument in call to '__builtin_memset' is a 
pointer to non-trivially copyable type 'struct nfp_nfd3_tx_desc' 
[-Wnontrivial-memaccess]
 95832 |  ({ size_t __fortify_size = (size_t)(tx_ring->size); 
fortify_memset_chk(__fortify_size, __builtin_dynamic_object_size(tx_ring->txds, 
0), __builtin_dynamic_object_size(tx_ring->txds, 1)), 
__builtin_memset(tx_ring->txds, 0, __fortify_size); });
   |

  ^
rings.i:95832:214: note: explicitly cast the pointer to silence this warning
 95832 |  ({ size_t __fortify_size = (size_t)(tx_ring->size); 
fortify_memset_chk(__fortify_size, __builtin_dynamic_object_size(tx_ring->txds, 
0), __builtin_dynamic_object_size(tx_ring->txds, 1)), 
__builtin_memset(tx_ring->txds, 0, __fortify_size); });
   |

  ^
   |

 (void*)
1 warning generated.
```

[rings.i.txt](https://github.com/user-attachments/files/17560453/rings.i.txt)

https://github.com/llvm/llvm-project/pull/111434
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PowerPC][ISelLowering] Support -mstack-protector-guard=tls (PR #110928)

2024-10-03 Thread Nathan Chancellor via cfe-commits


@@ -3605,7 +3605,8 @@ static void RenderSSPOptions(const Driver &D, const 
ToolChain &TC,
 StringRef Value = A->getValue();
 if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
 !EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
-!EffectiveTriple.isRISCV())
+!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC64() &&
+!EffectiveTriple.isPPC32())

nathanchance wrote:

Any reason not to use `EffectiveTriple.isPPC()` instead of both 
`EffectiveTriple.isPPC32()` and `EffectiveTriple.isPPC64()`?

https://github.com/llvm/llvm-project/pull/110928
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][counted_by] Refactor __builtin_dynamic_object_size on FAMs (PR #122198)

2025-01-31 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This breaks `ARCH=arm64 allmodconfig` for me.

```
$ echo CONFIG_WERROR=n >kernel/configs/no-werror.config

$ make -skj"$(nproc)" ARCH=arm64 LLVM=1 mrproper {allmod,no-werror.}config 
drivers/net/ethernet/sfc/falcon/falcon.o
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: 
/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang ...
1.   parser at end of file
2.  Per-file LLVM IR generation
3.  drivers/net/ethernet/sfc/falcon/falcon.c:1099:13: Generating code for 
declaration 'falcon_reconfigure_xmac_core'
4.  drivers/net/ethernet/sfc/falcon/falcon.c:1149:2 
: LLVM IR generation of 
compound statement ('{}')
 #0 0x55636ce677c8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x436f7c8)
 #1 0x55636ce652ee llvm::sys::RunSignalHandlers() 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x436d2ee)
 #2 0x55636cdea976 CrashRecoverySignalHandler(int) 
CrashRecoveryContext.cpp:0:0
 #3 0x7fe53564c1d0 (/usr/lib/libc.so.6+0x3d1d0)
 #4 0x55636d415238 
clang::CodeGen::CodeGenFunction::emitCountedByMemberSize(clang::Expr const*, 
llvm::Value*, unsigned int, llvm::IntegerType*) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x491d238)
 #5 0x55636d41489f 
clang::CodeGen::CodeGenFunction::emitBuiltinObjectSize(clang::Expr const*, 
unsigned int, llvm::IntegerType*, llvm::Value*, bool) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x491c89f)
 #6 0x55636d41de14 
clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned 
int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x4925e14)
 #8 0x55636d27dd2a (anonymous 
namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) 
CGExprScalar.cpp:0:0
 #9 0x55636d26a64b (anonymous 
namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
#10 0x55636d26a53d 
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x477253d)
#11 0x55636d2b42d0 
clang::CodeGen::CodeGenFunction::EmitScalarInit(clang::Expr const*, 
clang::ValueDecl const*, clang::CodeGen::LValue, bool) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x47bc2d0)
#12 0x55636d2b78dc 
clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction::AutoVarEmission
 const&) (/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x47bf8dc)
#13 0x55636d2b1c69 
clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x47b9c69)
#14 0x55636d2b14ef clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl 
const&) (/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x47b94ef)
#15 0x55636d1e146b 
clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46e946b)
#16 0x55636d1d4d53 
clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, 
llvm::ArrayRef) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46dcd53)
#17 0x55636d1d42f4 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt 
const*, llvm::ArrayRef) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46dc2f4)
#18 0x55636d1e2652 
clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt
 const&, bool, clang::CodeGen::AggValueSlot) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46ea652)
#19 0x55636d1e1399 
clang::CodeGen::CodeGenFunction::EmitCompoundStmt(clang::CompoundStmt const&, 
bool, clang::CodeGen::AggValueSlot) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46e9399)
#20 0x55636d2797e7 (anonymous 
namespace)::ScalarExprEmitter::VisitStmtExpr(clang::StmtExpr const*) 
CGExprScalar.cpp:0:0
#21 0x55636d26a53d 
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x477253d)
#22 0x55636d23cb84 
clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x4744b84)
#23 0x55636d1d43e7 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt 
const*, llvm::ArrayRef) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46dc3e7)
#24 0x55636d1e2830 
clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt
 const&, bool, clang::CodeGen::AggValueSlot) 
(/mnt/nvme/tmp/build/llvm-project-testing/final/bin/clang+0x46ea830)
#25 0x55636d1c0676 
clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, 
llvm::Function*, clang::CodeGen::CGFunctionInfo const&) 
(/mnt/nvme/tmp/build/llvm-project-t

[clang] [Clang][counted_by] Don't treat a __bdos argument as an array if it isn't (PR #125298)

2025-01-31 Thread Nathan Chancellor via cfe-commits


@@ -0,0 +1,85 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 5
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -DCOUNTED_BY -O2 -Wall 
-Wno-int-conversion -fsanitize=array-bounds,object-size,local-bounds 
-fstrict-flex-arrays=3 -emit-llvm -o - %s | FileCheck 
--check-prefix=SANITIZE-WITH-ATTR %s
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -DCOUNTED_BY -O2 -Wall 
-Wno-int-conversion -fsanitize=array-bounds,object-size,local-bounds 
-fstrict-flex-arrays=3 -emit-llvm -o - %s | FileCheck 
--check-prefix=NO-SANITIZE-WITH-ATTR %s
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O2 -Wall 
-Wno-int-conversion -fsanitize=array-bounds,object-size,local-bounds 
-fstrict-flex-arrays=3 -emit-llvm -o - %s | FileCheck 
--check-prefix=SANITIZE-WITHOUT-ATTR %s
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O2 -Wall 
-Wno-int-conversion -fsanitize=array-bounds,object-size,local-bounds 
-fstrict-flex-arrays=3 -emit-llvm -o - %s | FileCheck 
--check-prefix=NO-SANITIZE-WITHOUT-ATTR %s
+
+// See https://github.com/llvm/llvm-project/pull/122198#issuecomment-2627868702
+
+#if !__has_attribute(counted_by)
+#error "has attribute broken"
+#endif
+
+#ifdef COUNTED_BY
+#define __counted_by(member)   __attribute__((__counted_by__(member)))
+#else
+#define __counted_by(member)
+#endif
+
+#define __bdos(P)  __builtin_dynamic_object_size(P, 0)
+
+typedef long unsigned int size_t;
+
+struct test1_struct {
+  int a;
+  char *b;
+  char c[] __counted_by(a);
+} d;
+
+// SANITIZE-WITH-ATTR-LABEL: define dso_local i64 @test1(
+// SANITIZE-WITH-ATTR-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// SANITIZE-WITH-ATTR-NEXT:  [[ENTRY:.*:]]
+// SANITIZE-WITH-ATTR-NEXT:ret i64 -1
+//
+// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local i64 @test1(
+// NO-SANITIZE-WITH-ATTR-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// NO-SANITIZE-WITH-ATTR-NEXT:  [[ENTRY:.*:]]
+// NO-SANITIZE-WITH-ATTR-NEXT:ret i64 -1
+//
+// SANITIZE-WITHOUT-ATTR-LABEL: define dso_local i64 @test1(
+// SANITIZE-WITHOUT-ATTR-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// SANITIZE-WITHOUT-ATTR-NEXT:  [[ENTRY:.*:]]
+// SANITIZE-WITHOUT-ATTR-NEXT:ret i64 -1
+//
+// NO-SANITIZE-WITHOUT-ATTR-LABEL: define dso_local i64 @test1(
+// NO-SANITIZE-WITHOUT-ATTR-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// NO-SANITIZE-WITHOUT-ATTR-NEXT:  [[ENTRY:.*:]]
+// NO-SANITIZE-WITHOUT-ATTR-NEXT:ret i64 -1
+//
+size_t test1(void) {
+  return __builtin_dynamic_object_size(d.b[4], 0);
+}
+
+typedef struct {
+  char __padding[0];
+} spinlock_t;
+struct {
+  int priv_len;
+  spinlock_t addr_list_lock;
+  char *dev_addr;
+  char priv[] __attribute__((__counted_by__(priv_len)));
+} x;
+
+// SANITIZE-WITH-ATTR-LABEL: define dso_local i64 
@falcon_reconfigure_xmac_core(

nathanchance wrote:

Looks like this needs to be updated to `@test2(`?

https://github.com/llvm/llvm-project/pull/125298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][counted_by] Don't treat a __bdos argument as an array if it isn't (PR #125298)

2025-01-31 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

This tentatively looks good to me. Do you need a reduced test case for this? 
`cvise` gave me:

```c
typedef struct {
  char __padding[0];
} spinlock_t;
struct {
  int priv_len;
  spinlock_t addr_list_lock;
  char *dev_addr;
  char priv[] __attribute__((__counted_by__(priv_len)));
} falcon_reconfigure_xmac_core_efx;
void falcon_reconfigure_xmac_core() {
  long __q_size_field = __builtin_dynamic_object_size(
  &falcon_reconfigure_xmac_core_efx.dev_addr[4], 1);
  _Bool __ret_do_once = __q_size_field, __ret_cond = __ret_do_once;
  static _Bool __already_done;
  __ret_cond &&__already_done;
}
```

https://github.com/llvm/llvm-project/pull/125298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Diagnose tautological bounds checks (PR #120222)

2024-12-18 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

@nikic I noticed your comment on #118472 (the motivator for this change AFAICT):

> `-fwrapv` should already cover pointers

but it does not seem like this warning takes that into account (see the last 
example below)? I noticed a few instances of this warning in the Linux kernel 
but it sets `-fno-strict-overflow` for well defined overflow semantics it can 
depend on because it wants to avoid optimizations like the one that triggered 
this warning (`-fno-delete-null-pointer-checks` is a similarly used flag), so 
claiming the result of the checks are always false just does not seem to line 
up with reality. It seems like if the optimizer is going to respect this check 
and not remove it, this warning should not trigger either.

```c
int check(const int* foo, unsigned int idx)
{
return foo + idx < foo;
}
```

```
$ clang-19 -O2 -c test.c

$ llvm-objdump -dr test.o
...
 :
   0: 31 c0 xorl%eax, %eax
   2: c3retq

$ clang-19 -O2 -fno-strict-overflow -c test.o

$ llvm-objdump -dr test.o
...
 :
   0: 89 f0 movl%esi, %eax
   2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
   6: 31 c0 xorl%eax, %eax
   8: 48 39 f9  cmpq%rdi, %rcx
   b: 0f 92 c0  setb%al
   e: c3retq
```

```
$ gcc --version | head -1
gcc (GCC) 14.2.1 20240910

$ gcc -O2 -c test.c

$ llvm-objdump -dr test.o
...
 :
   0: 31 c0 xorl%eax, %eax
   2: c3retq

$ gcc -O2 -fno-strict-overflow -c test.c

$ llvm-objdump -dr test.o
...
 :
   0: 89 f6 movl%esi, %esi
   2: 48 8d 04 b7   leaq(%rdi,%rsi,4), %rax
   6: 48 39 f8  cmpq%rdi, %rax
   9: 0f 92 c0  setb%al
   c: 0f b6 c0  movzbl  %al, %eax
   f: c3retq
```

```
$ clang --version | head -1
ClangBuiltLinux clang version 20.0.0git 
(https://github.com/llvm/llvm-project.git 
99c2e3b78210a345afb1b5121f12b0e7bf923543)

$ clang -O2 -c test.c
test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
3 | return foo + idx < foo;
  |  ^
1 warning generated.

$ llvm-objdump -dr test.o
...
 :
   0: 31 c0 xorl%eax, %eax
   2: c3retq

$ clang -O2 -c -fno-strict-overflow test.c
test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
3 | return foo + idx < foo;
  |  ^
1 warning generated.

$ llvm-objdump -dr test.o
...
 :
   0: 89 f0 movl%esi, %eax
   2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
   6: 31 c0 xorl%eax, %eax
   8: 48 39 f9  cmpq%rdi, %rcx
   b: 0f 92 c0  setb%al
   e: c3retq
```

I tested something like:

```diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e06a092177ef..2dd7c5951d71 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11789,10 +11789,11 @@ static bool checkForArray(const Expr *E) {
 /// Detect patterns ptr + size >= ptr and ptr + size < ptr, where ptr is a
 /// pointer and size is an unsigned integer. Return whether the result is
 /// always true/false.
-static std::optional isTautologicalBoundsCheck(const Expr *LHS,
+static std::optional isTautologicalBoundsCheck(Sema &S,
+ const Expr *LHS,
  const Expr *RHS,
  BinaryOperatorKind Opc) {
-  if (!LHS->getType()->isPointerType())
+  if (!LHS->getType()->isPointerType() || 
S.getLangOpts().isSignedOverflowDefined())
 return std::nullopt;

   // Canonicalize to >= or < predicate.
@@ -11940,7 +11941,7 @@ static void diagnoseTautologicalComparison(Sema &S, 
SourceLocation Loc,
 << 1 /*array comparison*/
 << Result);
 } else if (std::optional Res =
-   isTautologicalBoundsCheck(LHS, RHS, Opc)) {
+   isTautologicalBoundsCheck(S, LHS, RHS, Opc)) {
   S.DiagRuntimeBehavior(Loc, nullptr,
 S.PDiag(diag::warn_comparison_always)
 << 2 /*pointer comparison*/
```

which results in a behavior I would expect:

```
$ clang -fsyntax-only test.c
test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wt

[clang] [Sema] Fix tautological bounds check warning with -fwrapv (PR #120480)

2024-12-18 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance updated 
https://github.com/llvm/llvm-project/pull/120480

>From 0eb68a5e438701a92dcedefc26a99c8dd48d0bed Mon Sep 17 00:00:00 2001
From: Nathan Chancellor 
Date: Wed, 18 Dec 2024 14:03:14 -0700
Subject: [PATCH 1/3] [Sema] Fix tautological bounds check warning with -fwrapv

The tautological bounds check warning added in #120222 does not take
into account whether signed integer overflow is well defined or not,
which could result in a developer removing a bounds check that may not
actually be always false because of different overflow semantics.

  $ cat test.c
  int check(const int* foo, unsigned int idx)
  {
  return foo + idx < foo;
  }

  $ clang -O2 -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check is eliminated without -fwrapv, warning was correct
  $ llvm-objdump -dr test.o
  ...
   :
 0: 31 c0 xorl%eax, %eax
 2: c3retq

  $ clang -O2 -fwrapv -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check remains, warning was wrong
  $ llvm-objdump -dr test.o
   :
 0: 89 f0 movl%esi, %eax
 2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
 6: 31 c0 xorl%eax, %eax
 8: 48 39 f9  cmpq%rdi, %rcx
 b: 0f 92 c0  setb%al
 e: c3retq

Prevent the warning from firing when -fwrapv is enabled.
---
 clang/lib/Sema/SemaExpr.cpp   |  7 +--
 .../Sema/tautological-pointer-comparison.c| 50 +++
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e06a092177ef02..24f7d27c691154 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11789,10 +11789,11 @@ static bool checkForArray(const Expr *E) {
 /// Detect patterns ptr + size >= ptr and ptr + size < ptr, where ptr is a
 /// pointer and size is an unsigned integer. Return whether the result is
 /// always true/false.
-static std::optional isTautologicalBoundsCheck(const Expr *LHS,
+static std::optional isTautologicalBoundsCheck(Sema &S, const Expr *LHS,
  const Expr *RHS,
  BinaryOperatorKind Opc) {
-  if (!LHS->getType()->isPointerType())
+  if (!LHS->getType()->isPointerType() ||
+  S.getLangOpts().isSignedOverflowDefined())
 return std::nullopt;
 
   // Canonicalize to >= or < predicate.
@@ -11940,7 +11941,7 @@ static void diagnoseTautologicalComparison(Sema &S, 
SourceLocation Loc,
 << 1 /*array comparison*/
 << Result);
 } else if (std::optional Res =
-   isTautologicalBoundsCheck(LHS, RHS, Opc)) {
+   isTautologicalBoundsCheck(S, LHS, RHS, Opc)) {
   S.DiagRuntimeBehavior(Loc, nullptr,
 S.PDiag(diag::warn_comparison_always)
 << 2 /*pointer comparison*/
diff --git a/clang/test/Sema/tautological-pointer-comparison.c 
b/clang/test/Sema/tautological-pointer-comparison.c
index 19cd20e5f7d21c..22734ecab6a2f3 100644
--- a/clang/test/Sema/tautological-pointer-comparison.c
+++ b/clang/test/Sema/tautological-pointer-comparison.c
@@ -1,40 +1,72 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -DFWRAPV -fwrapv -verify %s
+
+#ifdef FWRAPV
+// expected-no-diagnostics
+#endif
 
 int add_ptr_idx_ult_ptr(const char *ptr, unsigned index) {
-  return ptr + index < ptr; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return ptr + index < ptr;
 }
 
 int add_idx_ptr_ult_ptr(const char *ptr, unsigned index) {
-  return index + ptr < ptr; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return index + ptr < ptr;
 }
 
 int ptr_ugt_add_ptr_idx(const char *ptr, unsigned index) {
-  return ptr > ptr + index; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return ptr > ptr + index;
 }
 
 int ptr_ugt_add_idx_ptr(const char *ptr, unsigned index) {
-  return ptr > index + ptr; // expected-warning {{pointer comparison always 
evaluates to fals

[clang] [Sema] Fix tautological bounds check warning with -fwrapv (PR #120480)

2024-12-18 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance closed 
https://github.com/llvm/llvm-project/pull/120480
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Fix tautological bounds check warning with -fwrapv (PR #120480)

2024-12-18 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance created 
https://github.com/llvm/llvm-project/pull/120480

The tautological bounds check warning added in #120222 does not take into 
account whether signed integer overflow is well defined or not, which could 
result in a developer removing a bounds check that may not actually be always 
false because of different overflow semantics.

```c
int check(const int* foo, unsigned int idx)
{
return foo + idx < foo;
}
```

```
$ clang -O2 -c test.c
test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
3 | return foo + idx < foo;
  |  ^
1 warning generated.

# Bounds check is eliminated without -fwrapv, warning was correct
$ llvm-objdump -dr test.o
...
 :
   0: 31 c0 xorl%eax, %eax
   2: c3retq
```

```
$ clang -O2 -fwrapv -c test.c
test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
3 | return foo + idx < foo;
  |  ^
1 warning generated.

# Bounds check remains, warning was wrong
$ llvm-objdump -dr test.o
 :
   0: 89 f0 movl%esi, %eax
   2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
   6: 31 c0 xorl%eax, %eax
   8: 48 39 f9  cmpq%rdi, %rcx
   b: 0f 92 c0  setb%al
   e: c3retq
```

Prevent the warning from firing when `-fwrapv` is enabled.


>From 0eb68a5e438701a92dcedefc26a99c8dd48d0bed Mon Sep 17 00:00:00 2001
From: Nathan Chancellor 
Date: Wed, 18 Dec 2024 14:03:14 -0700
Subject: [PATCH] [Sema] Fix tautological bounds check warning with -fwrapv

The tautological bounds check warning added in #120222 does not take
into account whether signed integer overflow is well defined or not,
which could result in a developer removing a bounds check that may not
actually be always false because of different overflow semantics.

  $ cat test.c
  int check(const int* foo, unsigned int idx)
  {
  return foo + idx < foo;
  }

  $ clang -O2 -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check is eliminated without -fwrapv, warning was correct
  $ llvm-objdump -dr test.o
  ...
   :
 0: 31 c0 xorl%eax, %eax
 2: c3retq

  $ clang -O2 -fwrapv -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check remains, warning was wrong
  $ llvm-objdump -dr test.o
   :
 0: 89 f0 movl%esi, %eax
 2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
 6: 31 c0 xorl%eax, %eax
 8: 48 39 f9  cmpq%rdi, %rcx
 b: 0f 92 c0  setb%al
 e: c3retq

Prevent the warning from firing when -fwrapv is enabled.
---
 clang/lib/Sema/SemaExpr.cpp   |  7 +--
 .../Sema/tautological-pointer-comparison.c| 50 +++
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e06a092177ef02..24f7d27c691154 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11789,10 +11789,11 @@ static bool checkForArray(const Expr *E) {
 /// Detect patterns ptr + size >= ptr and ptr + size < ptr, where ptr is a
 /// pointer and size is an unsigned integer. Return whether the result is
 /// always true/false.
-static std::optional isTautologicalBoundsCheck(const Expr *LHS,
+static std::optional isTautologicalBoundsCheck(Sema &S, const Expr *LHS,
  const Expr *RHS,
  BinaryOperatorKind Opc) {
-  if (!LHS->getType()->isPointerType())
+  if (!LHS->getType()->isPointerType() ||
+  S.getLangOpts().isSignedOverflowDefined())
 return std::nullopt;
 
   // Canonicalize to >= or < predicate.
@@ -11940,7 +11941,7 @@ static void diagnoseTautologicalComparison(Sema &S, 
SourceLocation Loc,
 << 1 /*array comparison*/
 << Result);
 } else if (std::optional Res =
-   isTautologicalBoundsCheck(LHS, RHS, Opc)) {
+   isTautologicalBoundsCheck(S, LHS, RHS, Opc)) {
   S.DiagRuntimeBehavior(Loc, nullptr,
 S.PDiag(diag::warn_comparison_always)

[clang] [Sema] Diagnose tautological bounds checks (PR #120222)

2024-12-18 Thread Nathan Chancellor via cfe-commits

nathanchance wrote:

> @nathanchance You are correct, this warning should indeed respect 
> `-fwrapv`/`-fno-strict-overflow`. Your patch looks reasonable to me as well.

Thanks for the confirmation, I have submitted 
https://github.com/llvm/llvm-project/pull/120480 for this.

https://github.com/llvm/llvm-project/pull/120222
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Fix tautological bounds check warning with -fwrapv (PR #120480)

2024-12-18 Thread Nathan Chancellor via cfe-commits

https://github.com/nathanchance updated 
https://github.com/llvm/llvm-project/pull/120480

>From 0eb68a5e438701a92dcedefc26a99c8dd48d0bed Mon Sep 17 00:00:00 2001
From: Nathan Chancellor 
Date: Wed, 18 Dec 2024 14:03:14 -0700
Subject: [PATCH 1/2] [Sema] Fix tautological bounds check warning with -fwrapv

The tautological bounds check warning added in #120222 does not take
into account whether signed integer overflow is well defined or not,
which could result in a developer removing a bounds check that may not
actually be always false because of different overflow semantics.

  $ cat test.c
  int check(const int* foo, unsigned int idx)
  {
  return foo + idx < foo;
  }

  $ clang -O2 -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check is eliminated without -fwrapv, warning was correct
  $ llvm-objdump -dr test.o
  ...
   :
 0: 31 c0 xorl%eax, %eax
 2: c3retq

  $ clang -O2 -fwrapv -c test.c
  test.c:3:19: warning: pointer comparison always evaluates to false 
[-Wtautological-compare]
  3 | return foo + idx < foo;
|  ^
  1 warning generated.

  # Bounds check remains, warning was wrong
  $ llvm-objdump -dr test.o
   :
 0: 89 f0 movl%esi, %eax
 2: 48 8d 0c 87   leaq(%rdi,%rax,4), %rcx
 6: 31 c0 xorl%eax, %eax
 8: 48 39 f9  cmpq%rdi, %rcx
 b: 0f 92 c0  setb%al
 e: c3retq

Prevent the warning from firing when -fwrapv is enabled.
---
 clang/lib/Sema/SemaExpr.cpp   |  7 +--
 .../Sema/tautological-pointer-comparison.c| 50 +++
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e06a092177ef02..24f7d27c691154 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11789,10 +11789,11 @@ static bool checkForArray(const Expr *E) {
 /// Detect patterns ptr + size >= ptr and ptr + size < ptr, where ptr is a
 /// pointer and size is an unsigned integer. Return whether the result is
 /// always true/false.
-static std::optional isTautologicalBoundsCheck(const Expr *LHS,
+static std::optional isTautologicalBoundsCheck(Sema &S, const Expr *LHS,
  const Expr *RHS,
  BinaryOperatorKind Opc) {
-  if (!LHS->getType()->isPointerType())
+  if (!LHS->getType()->isPointerType() ||
+  S.getLangOpts().isSignedOverflowDefined())
 return std::nullopt;
 
   // Canonicalize to >= or < predicate.
@@ -11940,7 +11941,7 @@ static void diagnoseTautologicalComparison(Sema &S, 
SourceLocation Loc,
 << 1 /*array comparison*/
 << Result);
 } else if (std::optional Res =
-   isTautologicalBoundsCheck(LHS, RHS, Opc)) {
+   isTautologicalBoundsCheck(S, LHS, RHS, Opc)) {
   S.DiagRuntimeBehavior(Loc, nullptr,
 S.PDiag(diag::warn_comparison_always)
 << 2 /*pointer comparison*/
diff --git a/clang/test/Sema/tautological-pointer-comparison.c 
b/clang/test/Sema/tautological-pointer-comparison.c
index 19cd20e5f7d21c..22734ecab6a2f3 100644
--- a/clang/test/Sema/tautological-pointer-comparison.c
+++ b/clang/test/Sema/tautological-pointer-comparison.c
@@ -1,40 +1,72 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -DFWRAPV -fwrapv -verify %s
+
+#ifdef FWRAPV
+// expected-no-diagnostics
+#endif
 
 int add_ptr_idx_ult_ptr(const char *ptr, unsigned index) {
-  return ptr + index < ptr; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return ptr + index < ptr;
 }
 
 int add_idx_ptr_ult_ptr(const char *ptr, unsigned index) {
-  return index + ptr < ptr; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return index + ptr < ptr;
 }
 
 int ptr_ugt_add_ptr_idx(const char *ptr, unsigned index) {
-  return ptr > ptr + index; // expected-warning {{pointer comparison always 
evaluates to false}}
+#ifndef FWRAPV
+  // expected-warning@+2 {{pointer comparison always evaluates to false}}
+#endif
+  return ptr > ptr + index;
 }
 
 int ptr_ugt_add_idx_ptr(const char *ptr, unsigned index) {
-  return ptr > index + ptr; // expected-warning {{pointer comparison always 
evaluates to fals

[clang] [Sema] Fix tautological bounds check warning with -fwrapv (PR #120480)

2024-12-18 Thread Nathan Chancellor via cfe-commits


@@ -1,40 +1,72 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -DFWRAPV -fwrapv -verify %s

nathanchance wrote:

Ah yes, that is much cleaner. Done in 8504a0d758c06fa9f3b95117fdb05021d75afe8d.

https://github.com/llvm/llvm-project/pull/120480
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits