Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,10 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,

// Some ABIs may result in additional accesses to arguments that may
// otherwise not be present.
std::optional<llvm::Attribute::AttrKind> MemAttrForPtrArgs;
bool AddedPotentialArgAccess = false;
auto AddPotentialArgAccess = [&]() {
AddedPotentialArgAccess = true;
llvm::Attribute A = FuncAttrs.getAttribute(llvm::Attribute::Memory);
if (A.isValid())
FuncAttrs.addMemoryAttr(A.getMemoryEffects() |
Expand Down Expand Up @@ -2499,11 +2502,13 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
// gcc specifies that 'const' functions have greater restrictions than
// 'pure' functions, so they also cannot have infinite loops.
FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
MemAttrForPtrArgs = llvm::Attribute::ReadNone;
} else if (TargetDecl->hasAttr<PureAttr>()) {
FuncAttrs.addMemoryAttr(llvm::MemoryEffects::readOnly());
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
// gcc specifies that 'pure' functions cannot have infinite loops.
FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
MemAttrForPtrArgs = llvm::Attribute::ReadOnly;
} else if (TargetDecl->hasAttr<NoAliasAttr>()) {
FuncAttrs.addMemoryAttr(llvm::MemoryEffects::inaccessibleOrArgMemOnly());
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Expand Down Expand Up @@ -3011,6 +3016,27 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
}
assert(ArgNo == FI.arg_size());

ArgNo = 0;
if (AddedPotentialArgAccess && MemAttrForPtrArgs) {
llvm::FunctionType *FunctionType = FunctionType =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @qchateau

gcc warns on the extra FunctionType = here that I suppose should be removed:

../llvm-project/clang/lib/CodeGen/CGCall.cpp:3021:53: warning: operation on 'FunctionType' may be undefined [-Wsequence-point]
 3021 |     llvm::FunctionType *FunctionType = FunctionType =
      |                                        ~~~~~~~~~~~~~^
 3022 |         getTypes().GetFunctionType(FI);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

getTypes().GetFunctionType(FI);
for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
E = FI.arg_end();
I != E; ++I, ++ArgNo) {
if (I->info.isDirect() || I->info.isExpand() ||
I->info.isCoerceAndExpand()) {
unsigned FirstIRArg, NumIRArgs;
std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
for (unsigned i = FirstIRArg; i < FirstIRArg + NumIRArgs; ++i) {
if (FunctionType->getParamType(i)->isPointerTy()) {
ArgAttrs[i] =
ArgAttrs[i].addAttribute(getLLVMContext(), *MemAttrForPtrArgs);
}
}
}
}
}

AttrList = llvm::AttributeList::get(
getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
Expand Down
22 changes: 15 additions & 7 deletions clang/test/CodeGen/struct-passing.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ T0 __attribute__((const)) f0(void);
T0 __attribute__((pure)) f1(void);
T1 __attribute__((const)) f2(void);
T1 __attribute__((pure)) f3(void);
void __attribute__((const)) f4(T1 a);
void __attribute__((pure)) f5(T1 a);
int __attribute__((const)) f4(T1 a);
int __attribute__((pure)) f5(T1 a);

void *ps[] = { f0, f1, f2, f3, f4, f5 };
// NOTE: The int parameters verifies non-ptr parameters are not a problem
T1 __attribute__((const)) f6(void*, int);
T1 __attribute__((pure)) f7(void*, int);

void *ps[] = { f0, f1, f2, f3, f4, f5, f6, f7 };

// CHECK: declare i32 @f0() [[RN:#[0-9]+]]
// CHECK: declare i32 @f1() [[RO:#[0-9]+]]
// CHECK: declare void @f2({{.*}} sret({{.*}}) align 4)
// CHECK: declare void @f3({{.*}} sret({{.*}}) align 4)
// CHECK: declare void @f4({{.*}} byval({{.*}}) align 4)
// CHECK: declare void @f5({{.*}} byval({{.*}}) align 4)
// CHECK: declare void @f2(ptr {{[^,]*}} sret({{[^)]*}}) align 4) [[RNRW:#[0-9]+]]
// CHECK: declare void @f3(ptr {{[^,]*}} sret({{[^)]*}}) align 4) [[RORW:#[0-9]+]]
// CHECK: declare i32 @f4(ptr {{[^,]*}} byval({{[^)]*}}) align 4) [[RNRW:#[0-9]+]]
// CHECK: declare i32 @f5(ptr {{[^,]*}} byval({{[^)]*}}) align 4) [[RORW:#[0-9]+]]
// CHECK: declare void @f6(ptr {{[^,]*}} sret({{[^)]*}}) align 4, ptr {{[^,]*}} readnone, i32 {{[^,]*}}) [[RNRW:#[0-9]+]]
// CHECK: declare void @f7(ptr {{[^,]*}} sret({{[^)]*}}) align 4, ptr {{[^,]*}} readonly, i32 {{[^,]*}}) [[RORW:#[0-9]+]]

// CHECK: attributes [[RN]] = { nounwind willreturn memory(none){{.*}} }
// CHECK: attributes [[RO]] = { nounwind willreturn memory(read){{.*}} }
// CHECK: attributes [[RNRW]] = { nounwind willreturn memory(argmem: readwrite){{.*}} }
// CHECK: attributes [[RORW]] = { nounwind willreturn memory(read, argmem: readwrite){{.*}} }