Florian Ragwitz pushed to branch wip/rafl/hpc-tixdir at Glasgow Haskell Compiler / GHC Commits: 84362071 by Florian Ragwitz at 2026-10-17T17:27:32-07:00 Add a coverage flavour transformer - - - - - d494ba4d by Florian Ragwitz at 2026-10-17T17:33:31-07:00 hpc: Write tix data in a more compact format - - - - - b44b9e40 by Florian Ragwitz at 2026-10-17T18:33:52-07:00 Collect coverage information This is LLM slop. - - - - - 4 changed files: - .gitlab/ci.sh - .gitlab/generate-ci/gen_ci.hs - hadrian/src/Flavour.hs - rts/Hpc.c Changes: ===================================== .gitlab/ci.sh ===================================== @@ -54,6 +54,7 @@ Common Modes: clean Clean the tree shell Run an interactive shell with a configured build environment. save_test_output Generate unexpected-test-output.tar.gz + save_hpc_coverage Generate ghc-hpc-coverage.tar.gz save_cache Preserve the cabal cache build_hadrian Build GHC via the Hadrian build system test_hadrian Test GHC via the Hadrian build system @@ -649,6 +650,13 @@ function install_bindist() { } function test_hadrian() { + if [[ "${BUILD_FLAVOUR:-}" == *+coverage* ]]; then + export HPCTIX2=1 + # GHC invocations run in per-test working directories. + export HPCTIXDIR="$TOP/_build/ghc-tix" + mkdir -p "$HPCTIXDIR" + fi + check_msys2_deps _build/stage1/bin/ghc --version check_release_build @@ -898,6 +906,16 @@ function save_test_output() { tar -czf unexpected-test-output.tar.gz unexpected-test-output } +function save_hpc_coverage() { + # Keep whatever has been generated even if the build or tests failed. + if [[ -d _build/ghc-hpc || -d _build/ghc-tix ]]; then + mkdir -p _build/ghc-hpc _build/ghc-tix + tar -czf ghc-hpc-coverage.tar.gz _build/ghc-hpc _build/ghc-tix + else + warn "No HPC coverage directories found" + fi +} + function save_cache () { info "Storing cabal cache from $CABAL_DIR to $CABAL_CACHE..." rm -Rf "$CABAL_CACHE" @@ -934,7 +952,8 @@ function clean() { --exclude=ci_timings.txt \ --exclude=ghc-*.tar.xz \ --exclude=junit.xml \ - --exclude=unexpected-test-output.tar.gz + --exclude=unexpected-test-output.tar.gz \ + --exclude=ghc-hpc-coverage.tar.gz fi } ===================================== .gitlab/generate-ci/gen_ci.hs ===================================== @@ -998,7 +998,21 @@ delVariable k j = j { jobVariables = MonoidalMap $ Map.delete k $ unMonoidalMap -- | Make a normal validate CI job validate :: Arch -> Opsys -> BuildConfig -> NamedJob Job -validate = job +validate arch opsys bc = + let NamedJob n j = job arch opsys bc + covered = setVariable "BUILD_FLAVOUR" + (flavourString (mkJobFlavour bc) ++ "+coverage") j + saveCoverage = (if opsys == Windows then "bash " else "") + ++ ".gitlab/ci.sh save_hpc_coverage" + in NamedJob + { name = n + , jobInfo = covered + { jobAfterScript = saveCoverage : jobAfterScript covered + , jobArtifacts = (jobArtifacts covered) + { artifactPaths = artifactPaths (jobArtifacts covered) + ++ ["ghc-hpc-coverage.tar.gz"] } + } + } -- Nightly and release apply the FastCI configuration to all jobs so that they all run in -- the pipeline (not conditional on the full-ci label) ===================================== hadrian/src/Flavour.hs ===================================== @@ -86,6 +86,7 @@ flavourTransformers = M.fromList , "dump_stg" =: enableDumpStg , "hash_unit_ids" =: enableHashUnitIds , "hie_files" =: enableHieFiles + , "coverage" =: enableCoverage ] where (=:) = (,) @@ -383,6 +384,11 @@ enableHashUnitIds flavour = flavour { hashUnitIds = True } enableHieFiles :: Flavour -> Flavour enableHieFiles flavour = flavour { ghcHieFiles = (>= Stage1) } +enableCoverage :: Flavour -> Flavour +enableCoverage = + addArgs $ stage1 ? builder (Ghc CompileHs) ? packageOneOf [compiler, ghc] ? + pure ["-fhpc", "-hpcdir", "_build/ghc-hpc"] + -- | Build stage2 compiler with -fomit-interface-pragmas to reduce -- recompilation. omitPragmas :: Flavour -> Flavour ===================================== rts/Hpc.c ===================================== @@ -314,6 +314,57 @@ hs_hpc_module(char *modName, } } +static void writeULEB128(FILE *f, StgWord64 value) { + do { + unsigned int byte = (unsigned int)(value & 0x7f); + value >>= 7; + if (value != 0) byte |= 0x80; + fputc(byte, f); + } while (value != 0); +} + +static void writeLE32(FILE *f, StgWord32 value) { + for (unsigned int shift = 0; shift < 32; shift += 8) + fputc((int)((value >> shift) & 0xff), f); +} + +static void writeTix2(FILE *f) { + if (f == NULL) return; + + StgWord64 moduleCount = 0; + for (HpcModuleInfo *m = modules; m != NULL; m = m->next) + moduleCount++; + + fwrite("HPCTIX\0", 1, 7, f); + writeULEB128(f, moduleCount); + + for (HpcModuleInfo *m = modules; m != NULL; m = m->next) { + size_t nameLength = strlen(m->modName); + writeULEB128(f, (StgWord64)nameLength); + fwrite(m->modName, 1, nameLength, f); + writeLE32(f, m->hashNo); + writeULEB128(f, m->tickCount); + + const StgWord64 *counts = m->tixArr; + StgWord32 i = 0; + + while (i < m->tickCount) { + StgWord32 zeroStart = i; + while (i < m->tickCount && (counts == NULL || counts[i] == 0)) i++; + StgWord32 nonzeroStart = i; + while (i < m->tickCount && counts != NULL && counts[i] != 0) i++; + writeULEB128(f, nonzeroStart - zeroStart); + writeULEB128(f, i - nonzeroStart); + for (StgWord32 j = nonzeroStart; j < i; j++) + writeULEB128(f, counts[j]); + } + } + + bool writeFailed = ferror(f) != 0; + if (fclose(f) != 0 || writeFailed) + failure("could not write binary .tix file"); +} + static void writeTix(FILE *f) { HpcModuleInfo *tmpModule; @@ -387,7 +438,7 @@ static FILE *uniqueFile(const char *dir) { FILE *f = NULL; for (unsigned int i = 0; i < UINT_MAX; i++) { snprintf(name, (size_t)len + 1, fmt, dir, prog_name, (int)hpc_pid, i); - f = __rts_fopen(name, "wx"); + f = __rts_fopen(name, "wbx"); if (f != NULL || errno != EEXIST) break; } @@ -418,7 +469,8 @@ exitHpc(void) { #endif if (!is_subprocess && RtsFlags.HpcFlags.writeTixFile) { FILE *f = tixDir != NULL ? uniqueFile(tixDir) : __rts_fopen(tixFilename, "w+"); - writeTix(f); + if (getenv("HPCTIX2")) writeTix2(f); + else writeTix(f); } freeStrHashTable(moduleHash, (void (*)(void *))freeHpcModuleInfo); View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/4b9e24a9bba79cdd9d4549290af5c77... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/4b9e24a9bba79cdd9d4549290af5c77... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help