diff --git a/.github/workflows/llvm.yml b/.github/workflows/llvm.yml new file mode 100644 index 0000000000000..c18b3c27d547c --- /dev/null +++ b/.github/workflows/llvm.yml @@ -0,0 +1,34 @@ +name: CI + +on: [push] + +jobs: + build_llvm: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - windows-latest + - macOS-latest + cmake_args: + - "" + steps: + - name: Setup Windows + if: startsWith(matrix.os, 'windows') + uses: tstellar/actions/setup-windows@master + with: + arch: amd64 + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Install Ninja + uses: tstellar/actions/install-ninja@master + with: + os: ${{ runner.os }} + - name: Test LLVM + uses: tstellar/actions/build-test-llvm-project@master + with: + cmake_args: -G Ninja -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmake_args }} + os: ${{ runner.os }} diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index f4913540bab4d..8a51c9a19e0a9 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2272,6 +2272,9 @@ class FunctionDecl : public DeclaratorDecl, /// true through IsAligned. bool isReplaceableGlobalAllocationFunction(bool *IsAligned = nullptr) const; + /// Determine if this function provides the implementation of a system Builtin + bool isReplaceableSystemFunction() const; + /// Determine whether this is a destroying operator delete. bool isDestroyingOperatorDelete() const; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 0301110b7067f..b9b73bbe34817 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3003,6 +3003,15 @@ bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const return Params == FPT->getNumParams(); } +bool FunctionDecl::isReplaceableSystemFunction() const { + FunctionDecl const *Definition; + if (hasBody(Definition)) { + const SourceManager &SM = getASTContext().getSourceManager(); + return SM.isInSystemHeader(Definition->getLocation()); + } + return false; +} + bool FunctionDecl::isDestroyingOperatorDelete() const { // C++ P0722: // Within a class C, a single object deallocation function with signature @@ -3165,6 +3174,12 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { // function. Determine whether it actually refers to the C library // function or whether it just has the same name. + // If a system-level body was provided, use it instead of the intrinsic. Some + // C library do that to implement fortified version. + if (isReplaceableSystemFunction()) { + return 0; + } + // If this is a static function, it's not a builtin. if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) return 0; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 4959b80faec7a..5d266a127e5d4 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1831,6 +1831,11 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, else if (const auto *SA = FD->getAttr()) F->setSection(SA->getName()); + if (FD->isReplaceableSystemFunction()) { + F->addAttribute(llvm::AttributeList::FunctionIndex, + llvm::Attribute::NoBuiltin); + } + if (FD->isReplaceableGlobalAllocationFunction()) { // A replaceable global allocation function does not act like a builtin by // default, only if it is invoked by a new-expression or delete-expression. diff --git a/clang/test/CodeGen/memcpy-nobuitin.c b/clang/test/CodeGen/memcpy-nobuitin.c new file mode 100644 index 0000000000000..e4ad150a8aeda --- /dev/null +++ b/clang/test/CodeGen/memcpy-nobuitin.c @@ -0,0 +1,8 @@ +// RUN: clang -S -emit-llvm -o- %s -isystem . -DWITH_DECL | FileCheck --check-prefix=CHECK-WITH-DECL %s +// RUN: clang -S -emit-llvm -o- %s -isystem . -UWITH_DECL | FileCheck --check-prefix=CHECK-NO-DECL %s +// CHECK-WITH-DECL-NOT: @llvm.memcpy +// CHECK-NO-DECL: @llvm.memcpy +#include +void test(void* dest, void const* from, size_t n) { + memcpy(dest, from, n); +} diff --git a/clang/test/CodeGen/memcpy-nobuitin.inc b/clang/test/CodeGen/memcpy-nobuitin.inc new file mode 100644 index 0000000000000..62014513da0bd --- /dev/null +++ b/clang/test/CodeGen/memcpy-nobuitin.inc @@ -0,0 +1,12 @@ +#include +extern void* memcpy(void* dest, void const* from, size_t n); + +#ifdef WITH_DECL +inline void* memcpy(void* dest, void const* from, size_t n) { + char const* ifrom = from; + char * idest = dest; + while(n--) + *idest++ = *ifrom++; + return dest; +} +#endif