diff --git a/.ci/monolithic-windows.sh b/.ci/monolithic-windows.sh index 219979dd3e36e..5fb8f69528e89 100755 --- a/.ci/monolithic-windows.sh +++ b/.ci/monolithic-windows.sh @@ -23,8 +23,8 @@ runtimes_targets="${4}" start-group "CMake" pip install -q -r "${MONOREPO_ROOT}"/.ci/all_requirements.txt -export CC=cl -export CXX=cl +export CC=C:/clang/clang-msvc/bin/clang-cl.exe +export CXX=C:/clang/clang-msvc/bin/clang-cl.exe export LD=link # The CMAKE_*_LINKER_FLAGS to disable the manifest come from research @@ -49,6 +49,7 @@ cmake -S "${MONOREPO_ROOT}"/llvm -B "${BUILD_DIR}" \ -D CMAKE_EXE_LINKER_FLAGS="/MANIFEST:NO" \ -D CMAKE_MODULE_LINKER_FLAGS="/MANIFEST:NO" \ -D CMAKE_SHARED_LINKER_FLAGS="/MANIFEST:NO" \ + -D CMAKE_CXX_FLAGS="-Wno-c++98-compat -Wno-c++14-compat -Wno-unsafe-buffer-usage -Wno-old-style-cast" \ -D LLVM_ENABLE_RUNTIMES="${runtimes}" start-group "ninja" diff --git a/.ci/utils.sh b/.ci/utils.sh index dc8ce9b9a4214..540acfa8d5cc5 100644 --- a/.ci/utils.sh +++ b/.ci/utils.sh @@ -40,13 +40,17 @@ function at-exit { fi if [[ "$retcode" != "0" ]]; then - python "${MONOREPO_ROOT}"/.ci/premerge_advisor_upload.py \ - $(git rev-parse HEAD~1) $GITHUB_RUN_NUMBER \ - "${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log if [[ "$GITHUB_ACTIONS" != "" ]]; then python "${MONOREPO_ROOT}"/.ci/premerge_advisor_explain.py \ $(git rev-parse HEAD~1) "${BUILD_DIR}"/test-results.*.xml \ "${MONOREPO_ROOT}"/ninja*.log + python "${MONOREPO_ROOT}"/.ci/premerge_advisor_upload.py \ + $(git rev-parse HEAD~1) $GITHUB_RUN_NUMBER \ + "${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log + else + python "${MONOREPO_ROOT}"/.ci/premerge_advisor_upload.py \ + $(git rev-parse HEAD) $BUILDBOT_BUILDNUMBER \ + "${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log fi fi } diff --git a/.github/workflows/containers/github-action-ci-tooling/Dockerfile b/.github/workflows/containers/github-action-ci-tooling/Dockerfile index 9d2aaf6bbd48a..8aaa2e88f2bab 100644 --- a/.github/workflows/containers/github-action-ci-tooling/Dockerfile +++ b/.github/workflows/containers/github-action-ci-tooling/Dockerfile @@ -37,6 +37,14 @@ RUN apt-get update && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* +# Create a new user with id 1001 as that is the user id that +# Github Actions uses to perform the checkout action. +RUN useradd gha -u 1001 -m -s /bin/bash +RUN adduser gha sudo +RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers +# Don't set USER gha right away because we still need to install packages +# as root in 'ci-container-code-format' and 'ci-container-code-lint' containers + FROM base AS ci-container-code-format ARG LLVM_VERSION @@ -51,6 +59,8 @@ ENV PATH=${LLVM_SYSROOT}/bin:${PATH} COPY llvm/utils/git/requirements_formatting.txt requirements_formatting.txt RUN pip install -r requirements_formatting.txt --break-system-packages && \ rm requirements_formatting.txt +USER gha +WORKDIR /home/gha FROM base AS ci-container-code-lint @@ -80,3 +90,5 @@ RUN apt-get update && \ COPY llvm/utils/git/requirements_linting.txt requirements_linting.txt RUN pip install -r requirements_linting.txt --break-system-packages && \ rm requirements_linting.txt +USER gha +WORKDIR /home/gha diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml index 2b85d8b59869c..ac0689b4d3243 100644 --- a/.github/workflows/pr-code-format.yml +++ b/.github/workflows/pr-code-format.yml @@ -25,14 +25,6 @@ jobs: with: fetch-depth: 2 - # We need to set the repo checkout as safe, otherwise tj-actions/changed-files - # will fail due to the changed ownership inside the container. - # TODO(boomanaiden154): We should probably fix this by having the default user - # in the container have the same ID as the GHA user on the host. - - name: Set Safe Directory - run: | - chown -R root $(pwd) - - name: Get changed files id: changed-files uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5 diff --git a/.github/workflows/pr-code-lint.yml b/.github/workflows/pr-code-lint.yml index e67b518149c2c..8ba9378703739 100644 --- a/.github/workflows/pr-code-lint.yml +++ b/.github/workflows/pr-code-lint.yml @@ -31,11 +31,6 @@ jobs: with: fetch-depth: 2 - # FIXME: same as in ".github/workflows/pr-code-format.yml" - - name: Set Safe Directory - run: | - chown -R root $(pwd) - - name: Get changed files id: changed-files uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5 diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index e82b16f24c73f..fb22ad3c90af4 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -11,7 +11,7 @@ Introduction ============ The Clang Compiler is an open-source compiler for the C family of -programming languages, aiming to be the best in class implementation of +programming languages, aiming to be the best-in-class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for many targets. For more general information, please see the @@ -56,7 +56,7 @@ migration from GCC to Clang. In most cases, code "just works". Clang also provides an alternative driver, :ref:`clang-cl`, that is designed to be compatible with the Visual C++ compiler, cl.exe. -In addition to language specific features, Clang has a variety of +In addition to language-specific features, Clang has a variety of features that depend on what CPU architecture or operating system is being compiled for. Please see the :ref:`Target-Specific Features and Limitations ` section for more details. @@ -299,7 +299,7 @@ output format of the diagnostics that it generates. This option controls the output format of the filename, line number, and column printed in diagnostic messages. The options, and their - affect on formatting a simple conversion diagnostic, follow: + effect on formatting a simple conversion diagnostic, follow: **clang** (default) :: @@ -360,7 +360,7 @@ output format of the diagnostics that it generates. t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String] This category can be used by clients that want to group diagnostics - by category, so it should be a high level category. We want dozens + by category, so it should be a high-level category. We want dozens of these, not hundreds or thousands of them. .. _opt_fsave-optimization-record: @@ -750,7 +750,7 @@ control the crash diagnostics. Disable auto-generation of preprocessed source files during a clang crash. - The -fno-crash-diagnostics flag can be helpful for speeding the process + The ``-fno-crash-diagnostics`` flag can be helpful for speeding the process of generating a delta reduced test case. .. option:: -fcrash-diagnostics-dir= @@ -779,7 +779,7 @@ Options to Emit Optimization Reports ------------------------------------ Optimization reports trace, at a high-level, all the major decisions -done by compiler transformations. For instance, when the inliner +made by compiler transformations. For instance, when the inliner decides to inline function ``foo()`` into ``bar()``, or the loop unroller decides to unroll a loop N times, or the vectorizer decides to vectorize a loop body. @@ -845,11 +845,11 @@ compilations steps. .. option:: -fproc-stat-report= - This option requests driver to print used memory and execution time of each + This option requests the driver to print used memory and execution time of each compilation step. The ``clang`` driver during execution calls different tools, like compiler, assembler, linker etc. With this option the driver reports total execution time, the execution time spent in user mode and peak memory - usage of each the called tool. Value of the option specifies where the report + usage of each called tool. Value of the option specifies where the report is sent to. If it specifies a regular file, the data are saved to this file in CSV format: @@ -869,7 +869,7 @@ compilations steps. * peak memory usage in Kb. It is possible to specify this option without any value. In this case statistics - are printed on standard output in human readable format: + are printed on standard output in human-readable format: .. code-block:: console @@ -884,7 +884,7 @@ compilations steps. You can also use environment variables to control the process statistics reporting. Setting ``CC_PRINT_PROC_STAT`` to ``1`` enables the feature, the report goes to - stdout in human readable format. + stdout in human-readable format. Setting ``CC_PRINT_PROC_STAT_FILE`` to a fully qualified file path makes it report process statistics to the given file in the CSV format. Specifying a relative path will likely lead to multiple files with the same name created in different @@ -922,7 +922,7 @@ Clang options that don't fit neatly into other categories. most filenames can be written to the file without any special formatting. Different Make tools will treat different sets of characters as "special" and use different conventions for telling the Make tool that the character - is actually part of the filename. Normally Clang uses backslash to "escape" + is actually part of the filename. Normally, Clang uses backslash to "escape" a special character, which is the convention used by GNU Make. The -MV option tells Clang to put double-quotes around the entire filename, which is the convention used by NMake and Jom. @@ -957,7 +957,7 @@ Configuration files Configuration files group command-line options and allow all of them to be specified just by referencing the configuration file. They may be used, for -example, to collect options required to tune compilation for particular +example, to collect options required to tune compilation for a particular target, such as ``-L``, ``-I``, ``-l``, ``--sysroot``, codegen options, etc. Configuration files can be either specified on the command line or loaded @@ -986,7 +986,7 @@ either during build or during runtime. At build time, use ``CLANG_CONFIG_FILE_USER_DIR`` and ``CLANG_CONFIG_FILE_SYSTEM_DIR``. At run time use the ``--config-user-dir=`` and ``--config-system-dir=`` command line options. Specifying config directories at runtime overrides the config -directories set at build time The first file found is used. It is an error if +directories set at build time. The first file found is used. It is an error if the required file cannot be found. The default configuration files are searched for in the same directories @@ -996,7 +996,7 @@ the ``--no-default-config`` flag. First, the algorithm searches for a configuration file named ``-.cfg`` where `triple` is the triple for the target being -built for, and `driver` is the name of the currently used driver. The algorithm +built, and `driver` is the name of the currently used driver. The algorithm first attempts to use the canonical name for the driver used, then falls back to the one found in the executable name. @@ -1047,7 +1047,7 @@ It is not an error if either of these files is not found. The configuration file consists of command-line options specified on one or more lines. Lines composed of whitespace characters only are ignored as well as lines in which the first non-blank character is ``#``. Long options may be split -between several lines by a trailing backslash. Here is example of a +between several lines by a trailing backslash. Here is an example of a configuration file: :: @@ -1229,7 +1229,7 @@ Clang also allows you to push and pop the current warning state. This is particularly useful when writing a header file that will be compiled by other people, because you don't know what warning flags they build with. -In the below example :option:`-Wextra-tokens` is ignored for only a single line +In the example below, :option:`-Wextra-tokens` is ignored for only a single line of code, after which the diagnostics return to whatever state had previously existed. @@ -1253,7 +1253,7 @@ of warnings, so even when using GCC-compatible #pragmas there is no guarantee that they will have identical behaviour on both compilers. Clang also doesn't yet support GCC behavior for ``#pragma diagnostic pop`` -that doesn't have a corresponding ``#pragma diagnostic push``. In this case +that doesn't have a corresponding ``#pragma diagnostic push``. In this case, GCC pretends that there is a ``#pragma diagnostic push`` at the very beginning of the source file, so "unpaired" ``#pragma diagnostic pop`` matches that implicit push. This makes a difference for ``#pragma GCC diagnostic ignored`` @@ -1406,7 +1406,7 @@ project even if there are violations in some headers. # directory. But it'll still complain for all the other sources, e.g: $ cat foo/bar.cc #include "dir/include.h" // Clang flags unused declarations here. - #include "foo/include.h" // but unused warnings under this source is omitted. + #include "foo/include.h" // but unused warnings under this source are omitted. #include "next_to_bar_cc.h" // as are unused warnings from this header file. // Further, unused warnings in the remainder of bar.cc are also omitted. @@ -1648,7 +1648,7 @@ for more details. .. option:: -fno-fast-math - Disable fast-math mode. This options disables unsafe floating-point + Disable fast-math mode. This option disables unsafe floating-point optimizations by preventing the compiler from making any transformations that could affect the results. @@ -1766,7 +1766,7 @@ for more details. * ``fast``: enable fusion across statements disregarding pragmas, breaking compliance with the C and C++ standards (default for CUDA). - * ``on``: enable C and C++ standard complaint fusion in the same statement + * ``on``: enable C and C++ standard compliant fusion in the same statement unless dictated by pragmas (default for languages other than CUDA/HIP) * ``off``: disable fusion * ``fast-honor-pragmas``: fuse across statements unless dictated by pragmas @@ -1919,7 +1919,7 @@ for more details. a single expression of the code. Valid values are: ``source``, ``double``, and ``extended``. - For 64-bit targets, the default value is ``source``. For 32-bit x86 targets + For 64-bit targets, the default value is ``source``. For 32-bit x86 targets, however, in the case of NETBSD 6.99.26 and under, the default value is ``double``; in the case of NETBSD greater than 6.99.26, with NoSSE, the default value is ``extended``, with SSE the default value is ``source``. @@ -3881,9 +3881,9 @@ See :doc:`LanguageExtensions`. Differences between various standard modes ------------------------------------------ -clang supports the -std option, which changes what language mode clang uses. +clang supports the ``-std`` option, which changes what language mode clang uses. The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17, -gnu17, c23, gnu23, c2y, gnu2y, and various aliases for those modes. If no -std +gnu17, c23, gnu23, c2y, gnu2y, and various aliases for those modes. If no ``-std`` option is specified, clang defaults to gnu17 mode. Many C99 and C11 features are supported in earlier modes as a conforming extension, with a warning. Use ``-pedantic-errors`` to request an error if a feature from a later standard @@ -4609,7 +4609,7 @@ codebases. On ``x86_64-mingw32``, passing i128(by value) is incompatible with the Microsoft x64 calling convention. You might need to tweak -``WinX86_64ABIInfo::classify()`` in lib/CodeGen/Targets/X86.cpp. +``WinX86_64ABIInfo::classify()`` in ``lib/CodeGen/Targets/X86.cpp``. For the X86 target, clang supports the `-m16` command line argument which enables 16-bit code output. This is broadly similar to @@ -4760,8 +4760,8 @@ is imported, the linker will generate fixup code for reading or writing to the variable. When multiple toc-data options are used, the last option used has the affect. -For example: -mno-tocdata=g5,g1 -mtocdata=g1,g2 -mno-tocdata=g2 -mtocdata=g3,g4 -results in -mtocdata=g1,g3,g4 +For example: ``-mno-tocdata=g5,g1 -mtocdata=g1,g2 -mno-tocdata=g2 -mtocdata=g3,g4`` +results in ``-mtocdata=g1,g3,g4`` Names of variables not having external linkage will be ignored. @@ -5143,16 +5143,16 @@ Execute ``clang-cl /?`` to see a list of supported options: Instrument only functions from files where names match any regex separated by a semi-colon -fprofile-generate= Generate instrumented code to collect execution counts into a raw profile file in the directory specified by the argument. The filename uses default_%m.profraw pattern - (overridden by LLVM_PROFILE_FILE env var) + (overridden by ``LLVM_PROFILE_FILE`` env var) -fprofile-generate Generate instrumented code to collect execution counts into default_%m.profraw file - (overridden by '=' form of option or LLVM_PROFILE_FILE env var) + (overridden by '=' form of option or ``LLVM_PROFILE_FILE`` env var) -fprofile-instr-generate= Generate instrumented code to collect execution counts into the file whose name pattern is specified as the argument - (overridden by LLVM_PROFILE_FILE env var) + (overridden by ``LLVM_PROFILE_FILE`` env var) -fprofile-instr-generate Generate instrumented code to collect execution counts into default.profraw file - (overridden by '=' form of option or LLVM_PROFILE_FILE env var) + (overridden by '=' form of option or ``LLVM_PROFILE_FILE`` env var) -fprofile-instr-use= Use instrumentation data for coverage testing or profile-guided optimization -fprofile-use= diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h index 369ac3dc37b56..48ef8be9fb782 100644 --- a/clang/include/clang/CIR/MissingFeatures.h +++ b/clang/include/clang/CIR/MissingFeatures.h @@ -150,11 +150,9 @@ struct MissingFeatures { static bool zeroSizeRecordMembers() { return false; } // Coroutines - static bool coroAllocBuiltinCall() { return false; } - static bool coroBeginBuiltinCall() { return false; } static bool coroEndBuiltinCall() { return false; } - static bool coroSizeBuiltinCall() { return false; } static bool coroutineFrame() { return false; } + static bool emitBodyAndFallthrough() { return false; } // Various handling of deferred processing in CIRGenModule. static bool cgmRelease() { return false; } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 189798f71dbad..52904c72d1cfc 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2681,11 +2681,6 @@ class Sema final : public SemaBase { /// function without this attribute. bool DiscardingCFIUncheckedCallee(QualType From, QualType To) const; - /// Returns true if `From` is a function or pointer to a function without the - /// `cfi_unchecked_callee` attribute but `To` is a function or pointer to - /// function with this attribute. - bool AddingCFIUncheckedCallee(QualType From, QualType To) const; - /// This function calls Action when it determines that E designates a /// misaligned member due to the packed attribute. This is used to emit /// local diagnostics like in reference binding. diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 62fa04e15c717..e35100ffe4b6b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -449,10 +449,15 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, } case Builtin::BI__builtin_coro_free: case Builtin::BI__builtin_coro_size: { - cgm.errorNYI(e->getSourceRange(), - "BI__builtin_coro_free, BI__builtin_coro_size NYI"); - assert(!cir::MissingFeatures::coroSizeBuiltinCall()); - return getUndefRValue(e->getType()); + GlobalDecl gd{fd}; + mlir::Type ty = cgm.getTypes().getFunctionType( + cgm.getTypes().arrangeGlobalDeclaration(gd)); + const auto *nd = cast(gd.getDecl()); + cir::FuncOp fnOp = + cgm.getOrCreateCIRFunction(nd->getName(), ty, gd, /*ForVTable=*/false); + fnOp.setBuiltin(true); + return emitCall(e->getCallee()->getType(), CIRGenCallee::forDirect(fnOp), e, + returnValue); } case Builtin::BI__builtin_prefetch: { auto evaluateOperandAsInt = [&](const Expr *arg) { diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp index c25cce4ab33b3..8723a6e502b38 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp @@ -15,6 +15,7 @@ #include "clang/AST/StmtCXX.h" #include "clang/Basic/TargetInfo.h" #include "clang/CIR/Dialect/IR/CIRTypes.h" +#include "clang/CIR/MissingFeatures.h" using namespace clang; using namespace clang::CIRGen; @@ -23,6 +24,9 @@ struct clang::CIRGen::CGCoroData { // Stores the __builtin_coro_id emitted in the function so that we can supply // it as the first argument to other builtins. cir::CallOp coroId = nullptr; + + // Stores the result of __builtin_coro_begin call. + mlir::Value coroBegin = nullptr; }; // Defining these here allows to keep CGCoroData private to this file. @@ -63,6 +67,46 @@ cir::CallOp CIRGenFunction::emitCoroIDBuiltinCall(mlir::Location loc, nullPtr, nullPtr, nullPtr}); } +cir::CallOp CIRGenFunction::emitCoroAllocBuiltinCall(mlir::Location loc) { + cir::BoolType boolTy = builder.getBoolTy(); + + mlir::Operation *builtin = cgm.getGlobalValue(cgm.builtinCoroAlloc); + + cir::FuncOp fnOp; + if (!builtin) { + fnOp = cgm.createCIRBuiltinFunction(loc, cgm.builtinCoroAlloc, + cir::FuncType::get({UInt32Ty}, boolTy), + /*fd=*/nullptr); + assert(fnOp && "should always succeed"); + } else { + fnOp = cast(builtin); + } + + return builder.createCallOp( + loc, fnOp, mlir::ValueRange{curCoro.data->coroId.getResult()}); +} + +cir::CallOp +CIRGenFunction::emitCoroBeginBuiltinCall(mlir::Location loc, + mlir::Value coroframeAddr) { + mlir::Operation *builtin = cgm.getGlobalValue(cgm.builtinCoroBegin); + + cir::FuncOp fnOp; + if (!builtin) { + fnOp = cgm.createCIRBuiltinFunction( + loc, cgm.builtinCoroBegin, + cir::FuncType::get({UInt32Ty, VoidPtrTy}, VoidPtrTy), + /*fd=*/nullptr); + assert(fnOp && "should always succeed"); + } else { + fnOp = cast(builtin); + } + + return builder.createCallOp( + loc, fnOp, + mlir::ValueRange{curCoro.data->coroId.getResult(), coroframeAddr}); +} + mlir::LogicalResult CIRGenFunction::emitCoroutineBody(const CoroutineBodyStmt &s) { mlir::Location openCurlyLoc = getLoc(s.getBeginLoc()); @@ -73,10 +117,39 @@ CIRGenFunction::emitCoroutineBody(const CoroutineBodyStmt &s) { cir::CallOp coroId = emitCoroIDBuiltinCall(openCurlyLoc, nullPtrCst); createCoroData(*this, curCoro, coroId); - assert(!cir::MissingFeatures::coroAllocBuiltinCall()); - - assert(!cir::MissingFeatures::coroBeginBuiltinCall()); + // Backend is allowed to elide memory allocations, to help it, emit + // auto mem = coro.alloc() ? 0 : ... allocation code ...; + cir::CallOp coroAlloc = emitCoroAllocBuiltinCall(openCurlyLoc); + + // Initialize address of coroutine frame to null + CanQualType astVoidPtrTy = cgm.getASTContext().VoidPtrTy; + mlir::Type allocaTy = convertTypeForMem(astVoidPtrTy); + Address coroFrame = + createTempAlloca(allocaTy, getContext().getTypeAlignInChars(astVoidPtrTy), + openCurlyLoc, "__coro_frame_addr", + /*ArraySize=*/nullptr); + + mlir::Value storeAddr = coroFrame.getPointer(); + builder.CIRBaseBuilderTy::createStore(openCurlyLoc, nullPtrCst, storeAddr); + cir::IfOp::create( + builder, openCurlyLoc, coroAlloc.getResult(), + /*withElseRegion=*/false, + /*thenBuilder=*/[&](mlir::OpBuilder &b, mlir::Location loc) { + builder.CIRBaseBuilderTy::createStore( + loc, emitScalarExpr(s.getAllocate()), storeAddr); + cir::YieldOp::create(builder, loc); + }); + curCoro.data->coroBegin = + emitCoroBeginBuiltinCall( + openCurlyLoc, + cir::LoadOp::create(builder, openCurlyLoc, allocaTy, storeAddr)) + .getResult(); + + // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided. + if (s.getReturnStmtOnAllocFailure()) + cgm.errorNYI("handle coroutine return alloc failure"); assert(!cir::MissingFeatures::generateDebugInfo()); + assert(!cir::MissingFeatures::emitBodyAndFallthrough()); return mlir::success(); } diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index a8ffab79e8398..d7911302df45c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1332,6 +1332,9 @@ class CIRGenFunction : public CIRGenTypeCache { mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s); cir::CallOp emitCoroEndBuiltinCall(mlir::Location loc, mlir::Value nullPtr); cir::CallOp emitCoroIDBuiltinCall(mlir::Location loc, mlir::Value nullPtr); + cir::CallOp emitCoroAllocBuiltinCall(mlir::Location loc); + cir::CallOp emitCoroBeginBuiltinCall(mlir::Location loc, + mlir::Value coroframeAddr); void emitDestroy(Address addr, QualType type, Destroyer *destroyer); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 1fc116d98a858..186913d1bac9d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -496,6 +496,8 @@ class CIRGenModule : public CIRGenTypeCache { bool assumeConvergent = false); static constexpr const char *builtinCoroId = "__builtin_coro_id"; + static constexpr const char *builtinCoroAlloc = "__builtin_coro_alloc"; + static constexpr const char *builtinCoroBegin = "__builtin_coro_begin"; /// Given a builtin id for a function like "__builtin_fabsf", return a /// Function* for "fabsf". diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 1d0dfd0b9c151..a8a9c51952fbd 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3791,12 +3791,18 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, if (Current.is(TT_FunctionDeclarationName)) return true; - if (Current.isNoneOf(tok::identifier, tok::kw_operator)) + if (!Current.Tok.getIdentifierInfo()) return false; const auto *Prev = Current.getPreviousNonComment(); assert(Prev); + if (Prev->is(tok::coloncolon)) + Prev = Prev->Previous; + + if (!Prev) + return false; + const auto &Previous = *Prev; if (const auto *PrevPrev = Previous.getPreviousNonComment(); @@ -3845,8 +3851,6 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, // Find parentheses of parameter list. if (Current.is(tok::kw_operator)) { - if (Line.startsWith(tok::kw_friend)) - return true; if (Previous.Tok.getIdentifierInfo() && Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) { return true; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index ab893a5e9bf11..380a852207c21 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -12375,14 +12375,9 @@ static void DiagnoseMixedUnicodeImplicitConversion(Sema &S, const Type *Source, } } -enum CFIUncheckedCalleeChange { - None, - Adding, - Discarding, -}; - -static CFIUncheckedCalleeChange AdjustingCFIUncheckedCallee(QualType From, - QualType To) { +bool Sema::DiscardingCFIUncheckedCallee(QualType From, QualType To) const { + From = Context.getCanonicalType(From); + To = Context.getCanonicalType(To); QualType MaybePointee = From->getPointeeType(); if (!MaybePointee.isNull() && MaybePointee->getAs()) From = MaybePointee; @@ -12394,25 +12389,10 @@ static CFIUncheckedCalleeChange AdjustingCFIUncheckedCallee(QualType From, if (const auto *ToFn = To->getAs()) { if (FromFn->getCFIUncheckedCalleeAttr() && !ToFn->getCFIUncheckedCalleeAttr()) - return Discarding; - if (!FromFn->getCFIUncheckedCalleeAttr() && - ToFn->getCFIUncheckedCalleeAttr()) - return Adding; + return true; } } - return None; -} - -bool Sema::DiscardingCFIUncheckedCallee(QualType From, QualType To) const { - From = Context.getCanonicalType(From); - To = Context.getCanonicalType(To); - return ::AdjustingCFIUncheckedCallee(From, To) == Discarding; -} - -bool Sema::AddingCFIUncheckedCallee(QualType From, QualType To) const { - From = Context.getCanonicalType(From); - To = Context.getCanonicalType(To); - return ::AdjustingCFIUncheckedCallee(From, To) == Adding; + return false; } void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 06e5dab35cc3e..6d011239ec813 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -2533,15 +2533,12 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, SCS.setToType(2, FromType); - // If we have not converted the argument type to the parameter type, - // this is a bad conversion sequence, unless we're resolving an overload in C. - // - // Permit conversions from a function without `cfi_unchecked_callee` to a - // function with `cfi_unchecked_callee`. - if (CanonFrom == CanonTo || S.AddingCFIUncheckedCallee(CanonFrom, CanonTo)) + if (CanonFrom == CanonTo) return true; - if ((S.getLangOpts().CPlusPlus || !InOverloadResolution)) + // If we have not converted the argument type to the parameter type, + // this is a bad conversion sequence, unless we're resolving an overload in C. + if (S.getLangOpts().CPlusPlus || !InOverloadResolution) return false; ExprResult ER = ExprResult{From}; diff --git a/clang/test/CIR/CodeGen/coro-task.cpp b/clang/test/CIR/CodeGen/coro-task.cpp index 1fc7d77be2bce..265325f82d7f7 100644 --- a/clang/test/CIR/CodeGen/coro-task.cpp +++ b/clang/test/CIR/CodeGen/coro-task.cpp @@ -106,6 +106,9 @@ co_invoke_fn co_invoke; // CIR-NEXT: cir.global external @_ZN5folly4coro9co_invokeE = #cir.zero : !rec_folly3A3Acoro3A3Aco_invoke_fn // CIR: cir.func builtin private @__builtin_coro_id(!u32i, !cir.ptr, !cir.ptr, !cir.ptr) -> !u32i +// CIR: cir.func builtin private @__builtin_coro_alloc(!u32i) -> !cir.bool +// CIR: cir.func builtin private @__builtin_coro_size() -> !u64i +// CIR: cir.func builtin private @__builtin_coro_begin(!u32i, !cir.ptr) -> !cir.ptr using VoidTask = folly::coro::Task; @@ -114,10 +117,24 @@ VoidTask silly_task() { } // CIR: cir.func coroutine dso_local @_Z10silly_taskv() -> ![[VoidTask]] -// CHECK: %[[#VoidTaskAddr:]] = cir.alloca ![[VoidTask]], {{.*}}, ["__retval"] +// CIR: %[[VoidTaskAddr:.*]] = cir.alloca ![[VoidTask]], {{.*}}, ["__retval"] +// CIR: %[[SavedFrameAddr:.*]] = cir.alloca !cir.ptr, !cir.ptr>, ["__coro_frame_addr"] // Get coroutine id with __builtin_coro_id. // CIR: %[[NullPtr:.*]] = cir.const #cir.ptr : !cir.ptr // CIR: %[[Align:.*]] = cir.const #cir.int<16> : !u32i // CIR: %[[CoroId:.*]] = cir.call @__builtin_coro_id(%[[Align]], %[[NullPtr]], %[[NullPtr]], %[[NullPtr]]) + +// Perform allocation calling operator 'new' depending on __builtin_coro_alloc and +// call __builtin_coro_begin for the final coroutine frame address. + +// CIR: %[[ShouldAlloc:.*]] = cir.call @__builtin_coro_alloc(%[[CoroId]]) : (!u32i) -> !cir.bool +// CIR: cir.store{{.*}} %[[NullPtr]], %[[SavedFrameAddr]] : !cir.ptr, !cir.ptr> +// CIR: cir.if %[[ShouldAlloc]] { +// CIR: %[[CoroSize:.*]] = cir.call @__builtin_coro_size() : () -> !u64i +// CIR: %[[AllocAddr:.*]] = cir.call @_Znwm(%[[CoroSize]]) : (!u64i) -> !cir.ptr +// CIR: cir.store{{.*}} %[[AllocAddr]], %[[SavedFrameAddr]] : !cir.ptr, !cir.ptr> +// CIR: } +// CIR: %[[Load0:.*]] = cir.load{{.*}} %[[SavedFrameAddr]] : !cir.ptr>, !cir.ptr +// CIR: %[[CoroFrameAddr:.*]] = cir.call @__builtin_coro_begin(%[[CoroId]], %[[Load0]]) diff --git a/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp b/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp index 072f217ff7b19..a5a17dd5a4d82 100644 --- a/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp +++ b/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp @@ -9,6 +9,7 @@ void (*checked_ptr)(void) = unchecked; // expected-warning{{implicit conversion void (CFI_UNCHECKED_CALLEE *unchecked_ptr)(void) = unchecked; void (CFI_UNCHECKED_CALLEE *from_normal)(void) = checked; void (CFI_UNCHECKED_CALLEE *c_no_function_decay)(void) = &unchecked; +void (CFI_UNCHECKED_CALLEE __attribute__((noreturn)) *other_conflict)(void) = &checked; // expected-error{{cannot initialize a variable of type 'void (*)() __attribute__((noreturn)) __attribute__((cfi_unchecked_callee))' with an rvalue of type 'void (*)()'}} void (CFI_UNCHECKED_CALLEE *arr[10])(void); void (*cfi_elem)(void) = arr[1]; // expected-warning{{implicit conversion from 'void (*)() __attribute__((cfi_unchecked_callee))' to 'void (*)()' discards 'cfi_unchecked_callee' attribute}} void (CFI_UNCHECKED_CALLEE *cfi_unchecked_elem)(void) = arr[1]; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index ca99940890984..f3637383a0a65 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1129,11 +1129,6 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) { ASSERT_EQ(Tokens.size(), 7u) << Tokens; // Not TT_FunctionDeclarationName. EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown); - - Tokens = annotate("SomeAPI::operator()();"); - ASSERT_EQ(Tokens.size(), 9u) << Tokens; - // Not TT_FunctionDeclarationName. - EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown); } TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) { diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h index be64ef3770c60..bb47f31060885 100644 --- a/flang/include/flang/Parser/parse-tree.h +++ b/flang/include/flang/Parser/parse-tree.h @@ -3274,13 +3274,13 @@ struct FunctionReference { // R1521 call-stmt -> CALL procedure-designator [ chevrons ] // [( [actual-arg-spec-list] )] // (CUDA) chevrons -> <<< * | scalar-expr, scalar-expr [, -// scalar-int-expr [, scalar-int-expr ] ] >>> +// scalar-expr [, scalar-int-expr ] ] >>> struct CallStmt { BOILERPLATE(CallStmt); WRAPPER_CLASS(StarOrExpr, std::optional); struct Chevrons { TUPLE_CLASS_BOILERPLATE(Chevrons); - std::tuple, + std::tuple, std::optional> t; }; diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp index 92c0a64b39a9d..740dbbfab9db7 100644 --- a/flang/lib/Parser/program-parsers.cpp +++ b/flang/lib/Parser/program-parsers.cpp @@ -484,7 +484,7 @@ constexpr auto starOrExpr{ applyFunction(presentOptional, scalarExpr))}; TYPE_PARSER(extension( "<<<" >> construct(starOrExpr, ", " >> scalarExpr, - maybe("," >> scalarIntExpr), maybe("," >> scalarIntExpr)) / + maybe("," >> scalarExpr), maybe("," >> scalarIntExpr)) / ">>>")) constexpr auto actualArgSpecList{optionalList(actualArgSpec)}; TYPE_CONTEXT_PARSER("CALL statement"_en_US, diff --git a/flang/test/Lower/CUDA/cuda-kernel-calls.cuf b/flang/test/Lower/CUDA/cuda-kernel-calls.cuf index 71e594e4742ec..e0941f74072ba 100644 --- a/flang/test/Lower/CUDA/cuda-kernel-calls.cuf +++ b/flang/test/Lower/CUDA/cuda-kernel-calls.cuf @@ -16,6 +16,7 @@ contains subroutine host() real, device :: a integer(8) :: stream + integer(4) :: nbytes ! CHECK-LABEL: func.func @_QMtest_callPhost() ! CHECK: %[[A:.*]]:2 = hlfir.declare %{{.*}} {data_attr = #cuf.cuda, uniq_name = "_QMtest_callFhostEa"} : (!fir.ref) -> (!fir.ref, !fir.ref) @@ -57,6 +58,10 @@ contains call dev_kernel1<<<*,32,0,stream>>>(a) ! CHECK: cuf.kernel_launch @_QMtest_callPdev_kernel1<<<%c-1{{.*}}, %c1{{.*}}, %c1{{.*}}, %c32{{.*}}, %c1{{.*}}, %c1{{.*}}, %c0{{.*}}, %{{.*}} : !fir.ref>>>(%{{.*}}) : (!fir.ref) + call dev_kernel1<<<*, 32, 0.8 * nbytes>>>(a) +! CHECK: %[[MUL:.*]] = arith.mulf %{{.*}}, %{{.*}} fastmath : f32 +! CHECK: %[[BYTES:.*]] = fir.convert %[[MUL]] : (f32) -> i32 +! CHECK: cuf.kernel_launch @_QMtest_callPdev_kernel1<<<%{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[BYTES]]>>>(%{{.*}}) : (!fir.ref) end end diff --git a/flang/test/Parser/cuf-sanity-common b/flang/test/Parser/cuf-sanity-common index 816e03bed7220..2348c2edf3b73 100644 --- a/flang/test/Parser/cuf-sanity-common +++ b/flang/test/Parser/cuf-sanity-common @@ -43,6 +43,7 @@ module m call globalsub<<<1, 2>>> call globalsub<<<1, 2, 3>>> call globalsub<<<1, 2, 3, 4>>> + call globalsub<<<1, 2, 0.9*10, 4>>> call globalsub<<<*,5>>> allocate(pa(32), pinned = isPinned) end subroutine diff --git a/flang/test/Parser/cuf-sanity-tree.CUF b/flang/test/Parser/cuf-sanity-tree.CUF index 83d7540b8dec5..b4d53f27cf395 100644 --- a/flang/test/Parser/cuf-sanity-tree.CUF +++ b/flang/test/Parser/cuf-sanity-tree.CUF @@ -178,7 +178,7 @@ include "cuf-sanity-common" !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1' !CHECK: | | | | | | Scalar -> Expr = '2_4' !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2' -!CHECK: | | | | | | Scalar -> Integer -> Expr = '3_4' +!CHECK: | | | | | | Scalar -> Expr = '3_4' !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '3' !CHECK: | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> CallStmt = 'CALL globalsub<<<1_4,2_4,3_4,4_4>>>()' !CHECK: | | | | | Call @@ -188,10 +188,27 @@ include "cuf-sanity-common" !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1' !CHECK: | | | | | | Scalar -> Expr = '2_4' !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2' -!CHECK: | | | | | | Scalar -> Integer -> Expr = '3_4' +!CHECK: | | | | | | Scalar -> Expr = '3_4' !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '3' !CHECK: | | | | | | Scalar -> Integer -> Expr = '4_4' !CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '4' +!CHECK: | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> CallStmt = 'CALL globalsub<<<1_4,2_4,9._4,4_4>>>()' +!CHECK: | | | | | Call +!CHECK: | | | | | | ProcedureDesignator -> Name = 'globalsub' +!CHECK: | | | | | Chevrons +!CHECK: | | | | | | StarOrExpr -> Scalar -> Expr = '1_4' +!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1' +!CHECK: | | | | | | Scalar -> Expr = '2_4' +!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2' +!CHECK: | | | | | | Scalar -> Expr = '9._4' +!CHECK: | | | | | | | Multiply +!CHECK: | | | | | | | | Expr = '8.9999997615814208984375e-1_4' +!CHECK: | | | | | | | | | LiteralConstant -> RealLiteralConstant +!CHECK: | | | | | | | | | | Real = '0.9' +!CHECK: | | | | | | | | Expr = '10_4' +!CHECK: | | | | | | | | | LiteralConstant -> IntLiteralConstant = '10' +!CHECK: | | | | | | Scalar -> Integer -> Expr = '4_4' +!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '4' !CHECK: | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AllocateStmt !CHECK: | | | | | Allocation !CHECK: | | | | | | AllocateObject = 'pa' diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h index f4a09237ce897..c375df248154f 100644 --- a/lldb/include/lldb/Target/Target.h +++ b/lldb/include/lldb/Target/Target.h @@ -1356,7 +1356,11 @@ class Target : public std::enable_shared_from_this, StopHook(const StopHook &rhs); virtual ~StopHook() = default; - enum class StopHookKind : uint32_t { CommandBased = 0, ScriptBased }; + enum class StopHookKind : uint32_t { + CommandBased = 0, + ScriptBased, + CodeBased, + }; enum class StopHookResult : uint32_t { KeepStopped = 0, RequestContinue, @@ -1403,6 +1407,12 @@ class Target : public std::enable_shared_from_this, bool GetRunAtInitialStop() const { return m_at_initial_stop; } + void SetSuppressOutput(bool suppress_output) { + m_suppress_output = suppress_output; + } + + bool GetSuppressOutput() const { return m_suppress_output; } + void GetDescription(Stream &s, lldb::DescriptionLevel level) const; virtual void GetSubclassDescription(Stream &s, lldb::DescriptionLevel level) const = 0; @@ -1414,6 +1424,7 @@ class Target : public std::enable_shared_from_this, bool m_active = true; bool m_auto_continue = false; bool m_at_initial_stop = true; + bool m_suppress_output = false; StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid); }; @@ -1433,8 +1444,8 @@ class Target : public std::enable_shared_from_this, private: StringList m_commands; - // Use CreateStopHook to make a new empty stop hook. The GetCommandPointer - // and fill it with commands, and SetSpecifier to set the specifier shared + // Use CreateStopHook to make a new empty stop hook. Use SetActionFromString + // to fill it with commands, and SetSpecifier to set the specifier shared // pointer (can be null, that will match anything.) StopHookCommandLine(lldb::TargetSP target_sp, lldb::user_id_t uid) : StopHook(target_sp, uid) {} @@ -1460,19 +1471,56 @@ class Target : public std::enable_shared_from_this, StructuredDataImpl m_extra_args; lldb::ScriptedStopHookInterfaceSP m_interface_sp; - /// Use CreateStopHook to make a new empty stop hook. The GetCommandPointer - /// and fill it with commands, and SetSpecifier to set the specifier shared - /// pointer (can be null, that will match anything.) + /// Use CreateStopHook to make a new empty stop hook. Use SetScriptCallback + /// to set the script to execute, and SetSpecifier to set the specifier + /// shared pointer (can be null, that will match anything.) StopHookScripted(lldb::TargetSP target_sp, lldb::user_id_t uid) : StopHook(target_sp, uid) {} friend class Target; }; + class StopHookCoded : public StopHook { + public: + ~StopHookCoded() override = default; + + using HandleStopCallback = StopHookResult(ExecutionContext &exc_ctx, + lldb::StreamSP output); + + void SetCallback(llvm::StringRef name, HandleStopCallback *callback) { + m_name = name; + m_callback = callback; + } + + StopHookResult HandleStop(ExecutionContext &exc_ctx, + lldb::StreamSP output) override { + return m_callback(exc_ctx, output); + } + + void GetSubclassDescription(Stream &s, + lldb::DescriptionLevel level) const override { + s.Indent(); + s.Printf("%s (built-in)\n", m_name.c_str()); + } + + private: + std::string m_name; + HandleStopCallback *m_callback; + + /// Use CreateStopHook to make a new empty stop hook. Use SetCallback to set + /// the callback to execute, and SetSpecifier to set the specifier shared + /// pointer (can be null, that will match anything.) + StopHookCoded(lldb::TargetSP target_sp, lldb::user_id_t uid) + : StopHook(target_sp, uid) {} + friend class Target; + }; + + void RegisterInternalStopHooks(); + typedef std::shared_ptr StopHookSP; /// Add an empty stop hook to the Target's stop hook list, and returns a - /// shared pointer to it in new_hook. Returns the id of the new hook. - StopHookSP CreateStopHook(StopHook::StopHookKind kind); + /// shared pointer to the new hook. + StopHookSP CreateStopHook(StopHook::StopHookKind kind, bool internal = false); /// If you tried to create a stop hook, and that failed, call this to /// remove the stop hook, as it will also reset the stop hook counter. @@ -1484,8 +1532,6 @@ class Target : public std::enable_shared_from_this, // control over the process for the first time. bool RunStopHooks(bool at_initial_stop = false); - size_t GetStopHookSize(); - bool SetSuppresStopHooks(bool suppress) { bool old_value = m_suppress_stop_hooks; m_suppress_stop_hooks = suppress; @@ -1504,19 +1550,7 @@ class Target : public std::enable_shared_from_this, void SetAllStopHooksActiveState(bool active_state); - size_t GetNumStopHooks() const { return m_stop_hooks.size(); } - - StopHookSP GetStopHookAtIndex(size_t index) { - if (index >= GetNumStopHooks()) - return StopHookSP(); - StopHookCollection::iterator pos = m_stop_hooks.begin(); - - while (index > 0) { - pos++; - index--; - } - return (*pos).second; - } + const std::vector GetStopHooks(bool internal = false) const; lldb::PlatformSP GetPlatform() { return m_platform_sp; } @@ -1656,6 +1690,7 @@ class Target : public std::enable_shared_from_this, typedef std::map StopHookCollection; StopHookCollection m_stop_hooks; lldb::user_id_t m_stop_hook_next_id; + std::vector m_internal_stop_hooks; uint32_t m_latest_stop_hook_id; /// This records the last natural stop at /// which we ran a stop-hook. bool m_valid; diff --git a/lldb/source/Commands/CommandCompletions.cpp b/lldb/source/Commands/CommandCompletions.cpp index b2fc893e13fe3..c60d30326a3b4 100644 --- a/lldb/source/Commands/CommandCompletions.cpp +++ b/lldb/source/Commands/CommandCompletions.cpp @@ -777,13 +777,11 @@ void CommandCompletions::StopHookIDs(CommandInterpreter &interpreter, if (!target_sp) return; - const size_t num = target_sp->GetNumStopHooks(); - for (size_t idx = 0; idx < num; ++idx) { + for (auto &stophook_sp : target_sp->GetStopHooks()) { StreamString strm; // The value 11 is an offset to make the completion description looks // neater. strm.SetIndentLevel(11); - const Target::StopHookSP stophook_sp = target_sp->GetStopHookAtIndex(idx); stophook_sp->GetDescription(strm, lldb::eDescriptionLevelInitial); request.TryCompleteCurrentArg(std::to_string(stophook_sp->GetID()), strm.GetString()); diff --git a/lldb/source/Commands/CommandObjectBreakpoint.cpp b/lldb/source/Commands/CommandObjectBreakpoint.cpp index de0a7e7093411..5a5512610cd33 100644 --- a/lldb/source/Commands/CommandObjectBreakpoint.cpp +++ b/lldb/source/Commands/CommandObjectBreakpoint.cpp @@ -1114,9 +1114,7 @@ class CommandObjectBreakpointList : public CommandObjectParsed { CommandObjectBreakpointList(CommandInterpreter &interpreter) : CommandObjectParsed( interpreter, "breakpoint list", - "List some or all breakpoints at configurable levels of detail.", - nullptr) { - CommandArgumentData bp_id_arg; + "List some or all breakpoints at configurable levels of detail.") { // Define the first (and only) variant of this arg. AddSimpleArgumentList(eArgTypeBreakpointID, eArgRepeatOptional); diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp index c59d02812f328..8de6521e65b25 100644 --- a/lldb/source/Commands/CommandObjectTarget.cpp +++ b/lldb/source/Commands/CommandObjectTarget.cpp @@ -5223,33 +5223,72 @@ class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed { #pragma mark CommandObjectTargetStopHookList // CommandObjectTargetStopHookList +#define LLDB_OPTIONS_target_stop_hook_list +#include "CommandOptions.inc" class CommandObjectTargetStopHookList : public CommandObjectParsed { public: CommandObjectTargetStopHookList(CommandInterpreter &interpreter) : CommandObjectParsed(interpreter, "target stop-hook list", - "List all stop-hooks.", "target stop-hook list") {} + "List all stop-hooks.") {} ~CommandObjectTargetStopHookList() override = default; + Options *GetOptions() override { return &m_options; } + + class CommandOptions : public Options { + public: + CommandOptions() = default; + ~CommandOptions() override = default; + + Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, + ExecutionContext *execution_context) override { + Status error; + const int short_option = m_getopt_table[option_idx].val; + + switch (short_option) { + case 'i': + m_internal = true; + break; + default: + llvm_unreachable("Unimplemented option"); + } + + return error; + } + + void OptionParsingStarting(ExecutionContext *execution_context) override { + m_internal = false; + } + + llvm::ArrayRef GetDefinitions() override { + return llvm::ArrayRef(g_target_stop_hook_list_options); + } + + // Instance variables to hold the values for command options. + bool m_internal = false; + }; + protected: void DoExecute(Args &command, CommandReturnObject &result) override { Target &target = GetTarget(); - size_t num_hooks = target.GetNumStopHooks(); - if (num_hooks == 0) { - result.GetOutputStream().PutCString("No stop hooks.\n"); - } else { - for (size_t i = 0; i < num_hooks; i++) { - Target::StopHookSP this_hook = target.GetStopHookAtIndex(i); - if (i > 0) - result.GetOutputStream().PutCString("\n"); - this_hook->GetDescription(result.GetOutputStream(), - eDescriptionLevelFull); - } + bool printed_hook = false; + for (auto &hook : target.GetStopHooks(m_options.m_internal)) { + if (printed_hook) + result.GetOutputStream().PutCString("\n"); + hook->GetDescription(result.GetOutputStream(), eDescriptionLevelFull); + printed_hook = true; } + + if (!printed_hook) + result.GetOutputStream().PutCString("No stop hooks.\n"); + result.SetStatus(eReturnStatusSuccessFinishResult); } + +private: + CommandOptions m_options; }; #pragma mark CommandObjectMultiwordTargetStopHooks diff --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td index a9f054e1d3d45..ed061312e2bb4 100644 --- a/lldb/source/Commands/Options.td +++ b/lldb/source/Commands/Options.td @@ -77,7 +77,7 @@ let Command = "breakpoint list" in { // FIXME: We need to add an "internal" command, and then add this sort of // thing to it. But I need to see it for now, and don't want to wait. def blist_internal : Option<"internal", "i">, - Desc<"Show debugger ${i}nternal breakpoints">; + Desc<"Show debugger ${i}nternal breakpoints.">; def blist_brief : Option<"brief", "b">, Group<1>, Desc<"Give a ${b}rief description of the breakpoint (no " @@ -1686,7 +1686,7 @@ let Command = "target modules lookup" in { "match, if a best match is available.">; } -let Command = "target stop hook add" in { +let Command = "target stop_hook add" in { def target_stop_hook_add_one_liner : Option<"one-liner", "o">, GroupRange<1, 3>, @@ -1762,6 +1762,12 @@ let Command = "target stop hook add" in { "Defaults to true.">; } +let Command = "target stop_hook list" in { + def target_stop_hook_list_internal + : Option<"internal", "i">, + Desc<"Show debugger ${i}nternal stop hooks.">; +} + let Command = "thread backtrace" in { def thread_backtrace_count : Option<"count", "c">, Group<1>, diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp index e224a12e33463..d070c3d953d4a 100644 --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -183,8 +183,8 @@ Target::Target(Debugger &debugger, const ArchSpec &target_arch, m_watchpoint_list(), m_process_sp(), m_search_filter_sp(), m_image_search_paths(ImageSearchPathsChanged, this), m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0), - m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false), - m_is_dummy_target(is_dummy_target), + m_internal_stop_hooks(), m_latest_stop_hook_id(0), m_valid(true), + m_suppress_stop_hooks(false), m_is_dummy_target(is_dummy_target), m_target_unique_id(g_target_unique_id++), m_frame_recognizer_manager_up( std::make_unique()) { @@ -217,6 +217,7 @@ Target::~Target() { void Target::PrimeFromDummyTarget(Target &target) { m_stop_hooks = target.m_stop_hooks; m_stop_hook_next_id = target.m_stop_hook_next_id; + m_internal_stop_hooks = target.m_internal_stop_hooks; for (const auto &breakpoint_sp : target.m_breakpoint_list.Breakpoints()) { if (breakpoint_sp->IsInternal()) @@ -383,6 +384,7 @@ void Target::Destroy() { m_image_search_paths.Clear(notify); m_stop_hooks.clear(); m_stop_hook_next_id = 0; + m_internal_stop_hooks.clear(); m_suppress_stop_hooks = false; m_repl_map.clear(); Args signal_args; @@ -3041,8 +3043,9 @@ SourceManager &Target::GetSourceManager() { return *m_source_manager_up; } -Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind) { - lldb::user_id_t new_uid = ++m_stop_hook_next_id; +Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind, + bool internal) { + user_id_t new_uid = (internal ? LLDB_INVALID_UID : ++m_stop_hook_next_id); Target::StopHookSP stop_hook_sp; switch (kind) { case StopHook::StopHookKind::CommandBased: @@ -3051,8 +3054,14 @@ Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind) { case StopHook::StopHookKind::ScriptBased: stop_hook_sp.reset(new StopHookScripted(shared_from_this(), new_uid)); break; + case StopHook::StopHookKind::CodeBased: + stop_hook_sp.reset(new StopHookCoded(shared_from_this(), new_uid)); + break; } - m_stop_hooks[new_uid] = stop_hook_sp; + if (internal) + m_internal_stop_hooks.push_back(stop_hook_sp); + else + m_stop_hooks[new_uid] = stop_hook_sp; return stop_hook_sp; } @@ -3098,6 +3107,23 @@ void Target::SetAllStopHooksActiveState(bool active_state) { } } +// FIXME: Ideally we would like to return a `const &` (const reference) instead +// of creating copy here, but that is not possible due to different container +// types. In C++20, we should be able to use `std::ranges::views::values` to +// adapt the key-pair entries in the `std::map` (behind `StopHookCollection`) +// to avoid creating the copy. +const std::vector +Target::GetStopHooks(bool internal) const { + if (internal) + return m_internal_stop_hooks; + + std::vector stop_hooks; + for (auto &[_, hook] : m_stop_hooks) + stop_hooks.push_back(hook); + + return stop_hooks; +} + bool Target::RunStopHooks(bool at_initial_stop) { if (m_suppress_stop_hooks) return false; @@ -3111,16 +3137,20 @@ bool Target::RunStopHooks(bool at_initial_stop) { if (!(state == eStateStopped || state == eStateAttaching)) return false; - if (m_stop_hooks.empty()) - return false; + auto is_active = [at_initial_stop](StopHookSP hook) { + bool should_run_now = (!at_initial_stop || hook->GetRunAtInitialStop()); + return hook->IsActive() && should_run_now; + }; - bool no_active_hooks = - llvm::none_of(m_stop_hooks, [at_initial_stop](auto &p) { - bool should_run_now = - !at_initial_stop || p.second->GetRunAtInitialStop(); - return p.second->IsActive() && should_run_now; - }); - if (no_active_hooks) + // Create list of active internal and user stop hooks. + std::vector active_hooks; + llvm::copy_if(m_internal_stop_hooks, std::back_inserter(active_hooks), + is_active); + for (auto &[_, hook] : m_stop_hooks) { + if (is_active(hook)) + active_hooks.push_back(hook); + } + if (active_hooks.empty()) return false; // Make sure we check that we are not stopped because of us running a user @@ -3169,24 +3199,21 @@ bool Target::RunStopHooks(bool at_initial_stop) { StreamSP output_sp = m_debugger.GetAsyncOutputStream(); auto on_exit = llvm::make_scope_exit([output_sp] { output_sp->Flush(); }); - bool print_hook_header = (m_stop_hooks.size() != 1); - bool print_thread_header = (num_exe_ctx != 1); + size_t num_hooks_with_output = llvm::count_if( + active_hooks, [](auto h) { return !h->GetSuppressOutput(); }); + bool print_hook_header = (num_hooks_with_output > 1); + bool print_thread_header = (num_exe_ctx > 1); bool should_stop = false; bool requested_continue = false; - for (auto stop_entry : m_stop_hooks) { - StopHookSP cur_hook_sp = stop_entry.second; - if (!cur_hook_sp->IsActive()) - continue; - if (at_initial_stop && !cur_hook_sp->GetRunAtInitialStop()) - continue; - + for (auto cur_hook_sp : active_hooks) { bool any_thread_matched = false; for (auto exc_ctx : exc_ctx_with_reasons) { if (!cur_hook_sp->ExecutionContextPasses(exc_ctx)) continue; - if (print_hook_header && !any_thread_matched) { + bool suppress_output = cur_hook_sp->GetSuppressOutput(); + if (print_hook_header && !any_thread_matched && !suppress_output) { StreamString s; cur_hook_sp->GetDescription(s, eDescriptionLevelBrief); if (s.GetSize() != 0) @@ -3197,7 +3224,7 @@ bool Target::RunStopHooks(bool at_initial_stop) { any_thread_matched = true; } - if (print_thread_header) + if (print_thread_header && !suppress_output) output_sp->Printf("-- Thread %d\n", exc_ctx.GetThreadPtr()->GetIndexID()); diff --git a/lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py b/lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py index 8a321b2ff6324..0f40dfd09c958 100644 --- a/lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py +++ b/lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py @@ -40,7 +40,7 @@ def run_arch(self, exe, arch): launch_info = target.GetLaunchInfo() error = lldb.SBError() process = target.Launch(launch_info, error) - self.assertTrue(error.Success, str(error)) + self.assertTrue(error.Success(), str(error)) self.assertState(process.GetState(), lldb.eStateExited) self.assertIn("slice: {}".format(arch), process.GetSTDOUT(1000)) diff --git a/lldb/test/Shell/ExecControl/StopHook/stop-hook-list.test b/lldb/test/Shell/ExecControl/StopHook/stop-hook-list.test new file mode 100644 index 0000000000000..42d0a67c60dfa --- /dev/null +++ b/lldb/test/Shell/ExecControl/StopHook/stop-hook-list.test @@ -0,0 +1,70 @@ +# Test stop hook user ID assignment, ordering, and printing. +# +# RUN: %lldb -b -s %s | FileCheck %s + +# Create some stop hooks +target stop-hook add -o 'print "Hello"' +target stop-hook add -o 'print "world,"' +target stop-hook add -o 'print "nice"' +target stop-hook add -o 'print "weather"' +target stop-hook add -o 'print "today!"' + +# Print hooks +target stop-hook list + +# CHECK: (lldb) target stop-hook list +# CHECK: Hook: 1 +# CHECK: "Hello" +# CHECK: Hook: 2 +# CHECK: "world," +# CHECK: Hook: 3 +# CHECK: "nice" +# CHECK: Hook: 4 +# CHECK: "weather" +# CHECK: Hook: 5 +# CHECK: "today!" + +# Delete last hook, then add new one +target stop-hook delete 5 +target stop-hook add -o 'print "Sunshine,"' + +# Stop hook gets new user ID (it is not reused) +# CHECK: (lldb) target stop-hook add -o 'print "Sunshine,"' +# CHECK: Stop hook #6 added. + +target stop-hook list +# CHECK: (lldb) target stop-hook list +# CHECK: Hook: 4 +# CHECK-NOT: Hook: 5 +# CHECK: Hook: 6 + +# Add a few more hooks +target stop-hook add -o 'print "rain,"' +target stop-hook add -o 'print "and wind!"' +target stop-hook add -o 'print "It is all okay!"' +# CHECK: Stop hook #7 added. +# CHECK: Stop hook #8 added. +# CHECK: Stop hook #9 added. + +# Delete a few hooks +target stop-hook delete 1 +target stop-hook delete 3 +target stop-hook delete 7 +target stop-hook delete 9 + +# Check that the list is still well-ordered +target stop-hook list +# CHECK: (lldb) target stop-hook list +# CHECK-NOT: Hook: 1 +# CHECK: Hook: 2 +# CHECK: "world," +# CHECK-NOT: Hook: 3 +# CHECK: Hook: 4 +# CHECK: "weather" +# CHECK-NOT: Hook: 5 +# CHECK: Hook: 6 +# CHECK: "Sunshine," +# CHECK-NOT: Hook: 7 +# CHECK: Hook: 8 +# CHECK: "and wind!" +# CHECK-NOT: Hook: 9 diff --git a/llvm/docs/DirectX/DXILArchitecture.rst b/llvm/docs/DirectX/DXILArchitecture.rst index 32b1e72deae7c..bce7fdaa386ed 100644 --- a/llvm/docs/DirectX/DXILArchitecture.rst +++ b/llvm/docs/DirectX/DXILArchitecture.rst @@ -118,9 +118,10 @@ The passes to generate DXIL IR follow the flow: Each of these passes has a defined responsibility: #. DXILOpLowering translates LLVM intrinsic calls to dx.op calls. -#. DXILPrepare transforms the DXIL IR to be compatible with LLVM 3.7, and - inserts bitcasts to allow typed pointers to be inserted. -#. DXILTranslateMetadata emits the DXIL Metadata structures. +#. DXILPrepare updates functions in the DXIL IR to be compatible with LLVM 3.7, + namely removing attributes, and inserting bitcasts to allow typed pointers + to be inserted. +#. DXILTranslateMetadata transforms and emits all recognized DXIL Metadata. The passes to encode DXIL to binary in the DX Container follow the flow: diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst index 9cdac9c59fa9b..d99b5843c2133 100644 --- a/llvm/docs/ProgrammersManual.rst +++ b/llvm/docs/ProgrammersManual.rst @@ -2161,6 +2161,16 @@ that are not simple pointers (use :ref:`SmallPtrSet ` for pointers). Note that ``DenseSet`` has the same requirements for the value type that :ref:`DenseMap ` has. +.. _dss_radixtree: + +llvm/ADT/RadixTree.h +^^^^^^^^^^^^^^^^^^^^ + +``RadixTree`` is a trie-based data structure that stores range-like keys and +their associated values. It is particularly efficient for storing keys that +share common prefixes, as it can compress these prefixes to save memory. It +supports efficient search of matching prefixes. + .. _dss_sparseset: llvm/ADT/SparseSet.h diff --git a/llvm/include/llvm/ADT/IndexedMap.h b/llvm/include/llvm/ADT/IndexedMap.h index 55935a7afdab4..02193c79a6f0c 100644 --- a/llvm/include/llvm/ADT/IndexedMap.h +++ b/llvm/include/llvm/ADT/IndexedMap.h @@ -43,40 +43,40 @@ class IndexedMap { // is trivially copyable. using StorageT = SmallVector; - StorageT storage_; - T nullVal_ = T(); - ToIndexT toIndex_; + StorageT Storage; + T NullVal = T(); + ToIndexT ToIndex; public: IndexedMap() = default; - explicit IndexedMap(const T &val) : nullVal_(val) {} + explicit IndexedMap(const T &Val) : NullVal(Val) {} - typename StorageT::reference operator[](IndexT n) { - assert(toIndex_(n) < storage_.size() && "index out of bounds!"); - return storage_[toIndex_(n)]; + typename StorageT::reference operator[](IndexT N) { + assert(ToIndex(N) < Storage.size() && "index out of bounds!"); + return Storage[ToIndex(N)]; } - typename StorageT::const_reference operator[](IndexT n) const { - assert(toIndex_(n) < storage_.size() && "index out of bounds!"); - return storage_[toIndex_(n)]; + typename StorageT::const_reference operator[](IndexT N) const { + assert(ToIndex(N) < Storage.size() && "index out of bounds!"); + return Storage[ToIndex(N)]; } - void reserve(typename StorageT::size_type s) { storage_.reserve(s); } + void reserve(typename StorageT::size_type S) { Storage.reserve(S); } - void resize(typename StorageT::size_type s) { storage_.resize(s, nullVal_); } + void resize(typename StorageT::size_type S) { Storage.resize(S, NullVal); } - void clear() { storage_.clear(); } + void clear() { Storage.clear(); } - void grow(IndexT n) { - unsigned NewSize = toIndex_(n) + 1; - if (NewSize > storage_.size()) + void grow(IndexT N) { + unsigned NewSize = ToIndex(N) + 1; + if (NewSize > Storage.size()) resize(NewSize); } - bool inBounds(IndexT n) const { return toIndex_(n) < storage_.size(); } + bool inBounds(IndexT N) const { return ToIndex(N) < Storage.size(); } - typename StorageT::size_type size() const { return storage_.size(); } + typename StorageT::size_type size() const { return Storage.size(); } }; } // namespace llvm diff --git a/llvm/include/llvm/ADT/RadixTree.h b/llvm/include/llvm/ADT/RadixTree.h new file mode 100644 index 0000000000000..d3c44e4e6345c --- /dev/null +++ b/llvm/include/llvm/ADT/RadixTree.h @@ -0,0 +1,350 @@ +//===-- llvm/ADT/RadixTree.h - Radix Tree implementation --------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +//===----------------------------------------------------------------------===// +// +// This file implements a Radix Tree. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_RADIXTREE_H +#define LLVM_ADT_RADIXTREE_H + +#include "llvm/ADT/ADL.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/iterator.h" +#include "llvm/ADT/iterator_range.h" +#include +#include +#include +#include +#include +#include + +namespace llvm { + +/// \brief A Radix Tree implementation. +/// +/// A Radix Tree (also known as a compact prefix tree or radix trie) is a +/// data structure that stores a dynamic set or associative array where keys +/// are strings and values are associated with these keys. Unlike a regular +/// trie, the edges of a radix tree can be labeled with sequences of characters +/// as well as single characters. This makes radix trees more efficient for +/// storing sparse data sets, where many nodes in a regular trie would have +/// only one child. +/// +/// This implementation supports arbitrary key types that can be iterated over +/// (e.g., `std::string`, `std::vector`, `ArrayRef`). The key type +/// must provide `begin()` and `end()` for iteration. +/// +/// The tree stores `std::pair` as its value type. +/// +/// Example usage: +/// \code +/// llvm::RadixTree Tree; +/// Tree.emplace("apple", 1); +/// Tree.emplace("grapefruit", 2); +/// Tree.emplace("grape", 3); +/// +/// // Find prefixes +/// for (const auto &[Key, Value] : Tree.find_prefixes("grapefruit juice")) { +/// // pair will be {"grape", 3} +/// // pair will be {"grapefruit", 2} +/// llvm::outs() << Key << ": " << Value << "\n"; +/// } +/// +/// // Iterate over all elements +/// for (const auto &[Key, Value] : Tree) +/// llvm::outs() << Key << ": " << Value << "\n"; +/// \endcode +/// +/// \note +/// The `RadixTree` takes ownership of the `KeyType` and `T` objects +/// inserted into it. When an element is removed or the tree is destroyed, +/// these objects will be destructed. +/// However, if `KeyType` is a reference-like type, e.g., StringRef or range, +/// the user must guarantee that the referenced data has a lifetime longer than +/// the tree. +template class RadixTree { +public: + using key_type = KeyType; + using mapped_type = T; + using value_type = std::pair; + +private: + using KeyConstIteratorType = + decltype(adl_begin(std::declval())); + using KeyConstIteratorRangeType = iterator_range; + using KeyValueType = + remove_cvref_t()))>; + using ContainerType = std::list; + + /// Represents an internal node in the Radix Tree. + struct Node { + KeyConstIteratorRangeType Key{KeyConstIteratorType{}, + KeyConstIteratorType{}}; + std::vector Children; + + /// An iterator to the value associated with this node. + /// + /// If this node does not have a value (i.e., it's an internal node that + /// only serves as a path to other values), this iterator will be equal + /// to default constructed `ContainerType::iterator()`. + typename ContainerType::iterator Value; + + /// The first character of the Key. Used for fast child lookup. + KeyValueType KeyFront; + + Node() = default; + Node(const KeyConstIteratorRangeType &Key) + : Key(Key), KeyFront(*Key.begin()) { + assert(!Key.empty()); + } + + Node(Node &&) = default; + Node &operator=(Node &&) = default; + + Node(const Node &) = delete; + Node &operator=(const Node &) = delete; + + const Node *findChild(const KeyConstIteratorRangeType &Key) const { + if (Key.empty()) + return nullptr; + for (const Node &Child : Children) { + assert(!Child.Key.empty()); // Only root can be empty. + if (Child.KeyFront == *Key.begin()) + return &Child; + } + return nullptr; + } + + Node *findChild(const KeyConstIteratorRangeType &Query) { + const Node *This = this; + return const_cast(This->findChild(Query)); + } + + size_t countNodes() const { + size_t R = 1; + for (const Node &C : Children) + R += C.countNodes(); + return R; + } + + /// + /// Splits the current node into two. + /// + /// This function is used when a new key needs to be inserted that shares + /// a common prefix with the current node's key, but then diverges. + /// The current `Key` is truncated to the common prefix, and a new child + /// node is created for the remainder of the original node's `Key`. + /// + /// \param SplitPoint An iterator pointing to the character in the current + /// `Key` where the split should occur. + void split(KeyConstIteratorType SplitPoint) { + Node Child(make_range(SplitPoint, Key.end())); + Key = make_range(Key.begin(), SplitPoint); + + Children.swap(Child.Children); + std::swap(Value, Child.Value); + + Children.emplace_back(std::move(Child)); + } + }; + + /// Root always corresponds to the empty key, which is the shortest possible + /// prefix for everything. + Node Root; + ContainerType KeyValuePairs; + + /// Finds or creates a new tail or leaf node corresponding to the `Key`. + Node &findOrCreate(KeyConstIteratorRangeType Key) { + Node *Curr = &Root; + if (Key.empty()) + return *Curr; + + for (;;) { + auto [I1, I2] = llvm::mismatch(Key, Curr->Key); + Key = make_range(I1, Key.end()); + + if (I2 != Curr->Key.end()) { + // Match is partial. Either query is too short, or there is mismatching + // character. Split either way, and put new node in between of the + // current and its children. + Curr->split(I2); + + // Split was caused by mismatch, so `findChild` would fail. + break; + } + + Node *Child = Curr->findChild(Key); + if (!Child) + break; + + // Move to child with the same first character. + Curr = Child; + } + + if (Key.empty()) { + // The current node completely matches the key, return it. + return *Curr; + } + + // `Key` is a suffix of original `Key` unmatched by path from the `Root` to + // the `Curr`, and we have no candidate in the children to match more. + // Create a new one. + return Curr->Children.emplace_back(Key); + } + + /// + /// An iterator for traversing prefixes search results. + /// + /// This iterator is used by `find_prefixes` to traverse the tree and find + /// elements that are prefixes to the given key. It's a forward iterator. + /// + /// \tparam MappedType The type of the value pointed to by the iterator. + /// This will be `value_type` for non-const iterators + /// and `const value_type` for const iterators. + template + class IteratorImpl + : public iterator_facade_base, + std::forward_iterator_tag, MappedType> { + const Node *Curr = nullptr; + KeyConstIteratorRangeType Query{KeyConstIteratorType{}, + KeyConstIteratorType{}}; + + void findNextValid() { + while (Curr && Curr->Value == typename ContainerType::iterator()) + advance(); + } + + void advance() { + assert(Curr); + if (Query.empty()) { + Curr = nullptr; + return; + } + + Curr = Curr->findChild(Query); + if (!Curr) { + Curr = nullptr; + return; + } + + auto [I1, I2] = llvm::mismatch(Query, Curr->Key); + if (I2 != Curr->Key.end()) { + Curr = nullptr; + return; + } + Query = make_range(I1, Query.end()); + } + + friend class RadixTree; + IteratorImpl(const Node *C, const KeyConstIteratorRangeType &Q) + : Curr(C), Query(Q) { + findNextValid(); + } + + public: + IteratorImpl() = default; + + MappedType &operator*() const { return *Curr->Value; } + + IteratorImpl &operator++() { + advance(); + findNextValid(); + return *this; + } + + bool operator==(const IteratorImpl &Other) const { + return Curr == Other.Curr; + } + }; + +public: + RadixTree() = default; + RadixTree(RadixTree &&) = default; + RadixTree &operator=(RadixTree &&) = default; + + using prefix_iterator = IteratorImpl; + using const_prefix_iterator = IteratorImpl; + + using iterator = typename ContainerType::iterator; + using const_iterator = typename ContainerType::const_iterator; + + /// Returns true if the tree is empty. + bool empty() const { return KeyValuePairs.empty(); } + + /// Returns the number of elements in the tree. + size_t size() const { return KeyValuePairs.size(); } + + /// Returns the number of nodes in the tree. + /// + /// This function counts all internal nodes in the tree. It can be useful for + /// understanding the memory footprint or complexity of the tree structure. + size_t countNodes() const { return Root.countNodes(); } + + /// Returns an iterator to the first element. + iterator begin() { return KeyValuePairs.begin(); } + const_iterator begin() const { return KeyValuePairs.begin(); } + + /// Returns an iterator to the end of the tree. + iterator end() { return KeyValuePairs.end(); } + const_iterator end() const { return KeyValuePairs.end(); } + + /// Constructs and inserts a new element into the tree. + /// + /// This function constructs an element in place within the tree. If an + /// element with the same key already exists, the insertion fails and the + /// function returns an iterator to the existing element along with `false`. + /// Otherwise, the new element is inserted and the function returns an + /// iterator to the new element along with `true`. + /// + /// \param Key The key of the element to construct. + /// \param Args Arguments to forward to the constructor of the mapped_type. + /// \return A pair consisting of an iterator to the inserted element (or to + /// the element that prevented insertion) and a boolean value + /// indicating whether the insertion took place. + template + std::pair emplace(key_type &&Key, Ts &&...Args) { + // We want to make new `Node` to refer key in the container, not the one + // from the argument. + // FIXME: Determine that we need a new node, before expanding + // `KeyValuePairs`. + const value_type &NewValue = KeyValuePairs.emplace_front( + std::move(Key), T(std::forward(Args)...)); + Node &Node = findOrCreate(NewValue.first); + bool HasValue = Node.Value != typename ContainerType::iterator(); + if (!HasValue) + Node.Value = KeyValuePairs.begin(); + else + KeyValuePairs.pop_front(); + return {Node.Value, !HasValue}; + } + + /// + /// Finds all elements whose keys are prefixes of the given `Key`. + /// + /// This function returns an iterator range over all elements in the tree + /// whose keys are prefixes of the provided `Key`. For example, if the tree + /// contains "abcde", "abc", "abcdefgh", and `Key` is "abcde", this function + /// would return iterators to "abcde" and "abc". + /// + /// \param Key The key to search for prefixes of. + /// \return An `iterator_range` of `const_prefix_iterator`s, allowing + /// iteration over the found prefix elements. + /// \note The returned iterators reference the `Key` provided by the caller. + /// The caller must ensure that `Key` remains valid for the lifetime + /// of the iterators. + iterator_range + find_prefixes(const key_type &Key) const { + return iterator_range{ + const_prefix_iterator(&Root, KeyConstIteratorRangeType(Key)), + const_prefix_iterator{}}; + } +}; + +} // namespace llvm + +#endif // LLVM_ADT_RADIXTREE_H diff --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h index 4a9598c734dbf..1889b90c14126 100644 --- a/llvm/include/llvm/ADT/STLForwardCompat.h +++ b/llvm/include/llvm/ADT/STLForwardCompat.h @@ -126,7 +126,7 @@ struct detector>, Op, Args...> { template