diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -52,6 +52,7 @@ CODEGENOPT(DisableFree , 1, 0) ///< Don't free memory. CODEGENOPT(DiscardValueNames , 1, 0) ///< Discard Value Names from the IR (LLVMContext flag) +CODEGENOPT(DisableNoundefArgs , 1, 0) ///< Disable emitting `noundef` attributes on IR call arguments CODEGENOPT(DisableGCov , 1, 0) ///< Don't run the GCov pass, for testing. CODEGENOPT(DisableLLVMPasses , 1, 0) ///< Don't run any LLVM IR passes to get ///< the pristine IR generated by the diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3953,6 +3953,8 @@ HelpText<"Disable freeing of memory on exit">; def discard_value_names : Flag<["-"], "discard-value-names">, HelpText<"Discard value names in LLVM IR">; +def disable_noundef_args : Flag<["-"], "disable-noundef-args">, + HelpText<"Disable emitting noundef attribute in LLVM IR">; def load : Separate<["-"], "load">, MetaVarName<"">, HelpText<"Load the named plugin (dynamic shared object)">; def plugin : Separate<["-"], "plugin">, MetaVarName<"">, diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -1876,6 +1876,54 @@ llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr); } +static bool DetermineNoUndef(QualType QTy, CodeGenTypes &Types, + const llvm::DataLayout &DL, const ABIArgInfo &AI, + bool CheckCoerce = true) { + llvm::Type *Ty = Types.ConvertTypeForMem(QTy); + if (AI.getKind() == ABIArgInfo::Indirect) + return true; + if (AI.getKind() == ABIArgInfo::Extend) + return true; + if (!DL.typeSizeEqualsStoreSize(Ty)) + // TODO: This will result in a modest amount of values not marked noundef + // when they could be. We care about values that *invisibly* contain undef + // bits from the perspective of LLVM IR. + return false; + if (CheckCoerce && AI.canHaveCoerceToType()) { + llvm::Type *CoerceTy = AI.getCoerceToType(); + if (DL.getTypeSizeInBits(CoerceTy) > DL.getTypeSizeInBits(Ty)) + // If we're coercing to a type with a greater size than the canonical one, + // we're introducing new undef bits. + // Coercing to a type of smaller or equal size is ok, as we know that + // there's no internal padding (typeSizeEqualsStoreSize). + return false; + } + if (QTy->isExtIntType()) + return true; + if (QTy->isReferenceType()) + return true; + if (QTy->isNullPtrType()) + return false; + if (QTy->isMemberPointerType()) + // TODO: Some member pointers are `noundef`, but it depends on the ABI. For + // now, never mark them. + return false; + if (QTy->isScalarType()) { + if (const ComplexType *Complex = dyn_cast(QTy)) + return DetermineNoUndef(Complex->getElementType(), Types, DL, AI, false); + return true; + } + if (const VectorType *Vector = dyn_cast(QTy)) + return DetermineNoUndef(Vector->getElementType(), Types, DL, AI, false); + if (const MatrixType *Matrix = dyn_cast(QTy)) + return DetermineNoUndef(Matrix->getElementType(), Types, DL, AI, false); + if (const ArrayType *Array = dyn_cast(QTy)) + return DetermineNoUndef(Array->getElementType(), Types, DL, AI, false); + + // TODO: Some structs may be `noundef`, in specific situations. + return false; +} + /// Construct the IR attribute list of a function or call. /// /// When adding an attribute, please consider where it should be handled: @@ -2075,6 +2123,22 @@ QualType RetTy = FI.getReturnType(); const ABIArgInfo &RetAI = FI.getReturnInfo(); + const llvm::DataLayout &DL = getDataLayout(); + + bool HasCppLinkage = getLangOpts().CPlusPlus || getLangOpts().OpenCLCPlusPlus; + const FunctionDecl *FDecl; + if (TargetDecl && (FDecl = dyn_cast(TargetDecl))) { + HasCppLinkage &= !FDecl->isExternC(); + } + + // Determine if the return type could be partially undef + if (HasCppLinkage) { + if (!RetTy->isVoidType() && RetAI.getKind() != ABIArgInfo::Indirect && + DetermineNoUndef(RetTy, getTypes(), DL, RetAI)) { + RetAttrs.addAttribute(llvm::Attribute::NoUndef); + } + } + switch (RetAI.getKind()) { case ABIArgInfo::Extend: if (RetAI.isSignExt()) @@ -2160,6 +2224,12 @@ } } + // Decide whether the argument we're handling could be partially undef + bool ArgNoUndef = DetermineNoUndef(ParamType, getTypes(), DL, AI); + if (!CodeGenOpts.DisableNoundefArgs && ArgNoUndef) { + Attrs.addAttribute(llvm::Attribute::NoUndef); + } + // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we // have the corresponding parameter variable. It doesn't make // sense to do it here because parameters are so messed up. diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -924,6 +924,7 @@ Opts.DisableFree = Args.hasArg(OPT_disable_free); Opts.DiscardValueNames = Args.hasArg(OPT_discard_value_names); + Opts.DisableNoundefArgs = Args.hasArg(OPT_disable_noundef_args); Opts.DisableTailCalls = Args.hasArg(OPT_mdisable_tail_calls); Opts.NoEscapingBlockTailCalls = Args.hasArg(OPT_fno_escaping_block_tail_calls); diff --git a/clang/test/CodeGen/attr-noundef.cpp b/clang/test/CodeGen/attr-noundef.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-noundef.cpp @@ -0,0 +1,152 @@ +// RUN: %clang_cc1 -x c++ -S -emit-llvm %s -o - | FileCheck %s + +//************ Passing structs by value +// TODO: No structs may currently be marked noundef + +namespace check_structs { +struct Trivial { + int a; +}; +Trivial ret_trivial() { return {}; } +void pass_trivial(Trivial e) {} +// CHECK: define i32 @{{.*}}ret_trivial +// CHECK: define void @{{.*}}pass_trivial{{.*}}(i32 % + +struct NoCopy { + int a; + NoCopy(NoCopy &) = delete; +}; +NoCopy ret_nocopy() { return {}; } +void pass_nocopy(NoCopy e) {} +// CHECK: define void @{{.*}}ret_nocopy{{.*}}(%"struct.check_structs::NoCopy"* noalias sret align 4 % +// CHECK: define void @{{.*}}pass_nocopy{{.*}}(%"struct.check_structs::NoCopy"* noundef % + +struct Huge { + int a[1024]; +}; +Huge ret_huge() { return {}; } +void pass_huge(Huge h) {} +// CHECK: define void @{{.*}}ret_huge{{.*}}(%"struct.check_structs::Huge"* noalias sret align 4 % +// CHECK: define void @{{.*}}pass_huge{{.*}}(%"struct.check_structs::Huge"* noundef byval +} // namespace check_structs + +//************ Passing unions by value +// No unions may be marked noundef + +namespace check_unions { +union Trivial { + int a; +}; +Trivial ret_trivial() { return {}; } +void pass_trivial(Trivial e) {} +// CHECK: define i32 @{{.*}}ret_trivial +// CHECK: define void @{{.*}}pass_trivial{{.*}}(i32 % + +union NoCopy { + int a; + NoCopy(NoCopy &) = delete; +}; +NoCopy ret_nocopy() { return {}; } +void pass_nocopy(NoCopy e) {} +// CHECK: define void @{{.*}}ret_nocopy{{.*}}(%"union.check_unions::NoCopy"* noalias sret align 4 % +// CHECK: define void @{{.*}}pass_nocopy{{.*}}(%"union.check_unions::NoCopy"* noundef % +} // namespace check_unions + +//************ Passing `this` pointers +// `this` pointer must always be defined + +namespace check_this { +struct Object { + int data[]; + + Object() { + this->data[0] = 0; + } + int getData() { + return this->data[0]; + } + Object *getThis() { + return this; + } +}; + +void use_object() { + Object obj; + obj.getData(); + obj.getThis(); +} +// CHECK: define linkonce_odr void @{{.*}}Object{{.*}}(%"struct.check_this::Object"* noundef % +// CHECK: define linkonce_odr noundef i32 @{{.*}}Object{{.*}}getData{{.*}}(%"struct.check_this::Object"* noundef % +// CHECK: define linkonce_odr noundef %"struct.check_this::Object"* @{{.*}}Object{{.*}}getThis{{.*}}(%"struct.check_this::Object"* noundef % +} // namespace check_this + +//************ Passing vector types + +namespace check_vecs { +typedef int __attribute__((vector_size(12))) i32x3; +i32x3 ret_vec() { + return {}; +} +void pass_vec(i32x3 v) { +} + +// CHECK: define noundef <3 x i32> @{{.*}}ret_vec{{.*}}() +// CHECK: define void @{{.*}}pass_vec{{.*}}(<3 x i32> noundef % +} // namespace check_vecs + +//************ Passing exotic types +// Function/Array pointers, Function member / Data member pointers, nullptr_t, ExtInt types + +namespace check_exotic { +struct Object { + int mfunc(); + int mdata; +}; +typedef int Object::*mdptr; +typedef int (Object::*mfptr)(); +typedef decltype(nullptr) nullptr_t; +typedef int (*arrptr)[32]; +typedef int (*fnptr)(int); + +arrptr ret_arrptr() { + return nullptr; +} +fnptr ret_fnptr() { + return nullptr; +} +mdptr ret_mdptr() { + return nullptr; +} +mfptr ret_mfptr() { + return nullptr; +} +nullptr_t ret_npt() { + return nullptr; +} +void pass_npt(nullptr_t t) { +} +_ExtInt(3) ret_extint() { + return 0; +} +void pass_extint(_ExtInt(3) e) { +} +void pass_large_extint(_ExtInt(127) e) { +} + +// Pointers to arrays/functions are always noundef +// CHECK: define noundef [32 x i32]* @{{.*}}ret_arrptr{{.*}}() +// CHECK: define noundef i32 (i32)* @{{.*}}ret_fnptr{{.*}}() + +// Pointers to members are never noundef +// CHECK: define i64 @{{.*}}ret_mdptr{{.*}}() +// CHECK: define { i64, i64 } @{{.*}}ret_mfptr{{.*}}() + +// nullptr_t is never noundef +// CHECK: define i8* @{{.*}}ret_npt{{.*}}() +// CHECK: define void @{{.*}}pass_npt{{.*}}(i8* % + +// ExtInt is only noundef if it keeps its size +// CHECK: define noundef signext i3 @{{.*}}ret_extint{{.*}}() +// CHECK: define void @{{.*}}pass_extint{{.*}}(i3 noundef signext % +// CHECK: define void @{{.*}}pass_large_extint{{.*}}(i64 %{{.*}}, i64 % +} // namespace check_exotic diff --git a/clang/test/CodeGen/indirect-noundef.cpp b/clang/test/CodeGen/indirect-noundef.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/indirect-noundef.cpp @@ -0,0 +1,34 @@ +// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -O0 -emit-llvm -o - %s | FileCheck %s + +union u1 { + int val; +}; + +// CHECK: @indirect_callee_int_ptr = global i32 (i32)* +int (*indirect_callee_int_ptr)(int); +// CHECK: @indirect_callee_union_ptr = global i32 (i32)* +union u1 (*indirect_callee_union_ptr)(union u1); + +// CHECK-LABEL: define noundef i32 @{{.*}}indirect_callee_int{{.*}}(i32 noundef % +int indirect_callee_int(int a) { return a; } +// CHECK-LABEL: define i32 @{{.*}}indirect_callee_union{{.*}}(i32 % +union u1 indirect_callee_union(union u1 a) { + return a; +} + +int main() { + // CHECK: call noundef i32 @{{.*}}indirect_callee_int{{.*}}(i32 noundef 0) + indirect_callee_int(0); + // CHECK: call i32 @{{.*}}indirect_callee_union{{.*}}(i32 % + indirect_callee_union((union u1){0}); + + indirect_callee_int_ptr = indirect_callee_int; + indirect_callee_union_ptr = indirect_callee_union; + + // CHECK: call noundef i32 %{{.*}}(i32 noundef 0) + indirect_callee_int_ptr(0); + // CHECK: call i32 %{{.*}}(i32 % + indirect_callee_union_ptr((union u1){}); + + return 0; +}