Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2109] swiftc coverage (-profile-generate -profile-coverage-mapping) does not work on linux #44717

Closed
swift-ci opened this issue Jul 19, 2016 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code coverage Area → source tooling: code coverage compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2109
Radar None
Original Reporter urugang (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment
$ swiftc --version
Swift version 3.0-dev (LLVM c191431197, Clang c6195325c5, Swift add621a959)
Target: x86_64-unknown-linux-gnu
$ lsb_release  -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:        14.04
Codename:       trusty
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CodeCoverage
Assignee @vedantk
Priority Medium

md5: 5bb9fff17c57e393c24e7b53e9756a4b

Issue Description:

Swift source "hello.swift" has been compiled with "-profile-generate -profile-coverage-mapping", and generated executable file "hello".
It does not produce .profraw file after the program has been executed.

$ swiftenv global  DEVELOPMENT-SNAPSHOT-2016-06-20-a
$ ls
hello.swift
$ swiftc -o hello -profile-generate -profile-coverage-mapping hello.swift
$ ./hello
$ ls
hello  hello.swift
@swift-ci
Copy link
Collaborator Author

Comment by Rugang Chen (JIRA)

I found that variable "Registation" was not used, then profiler has not been initialized.

extern "C" {

#include "InstrProfiling.h"

COMPILER_RT_VISIBILITY int __llvm_profile_runtime;
}

namespace {

class RegisterRuntime {
public:
  RegisterRuntime() {
    __llvm_profile_register_write_file_atexit();
    __llvm_profile_initialize_file();
  }
};

RegisterRuntime Registration;

}

So I added option "-Xlinker -u__llvm_profile_runtime" to linker.

1 file changed, 2 insertions(+)
lib/Driver/ToolChains.cpp | 2 ++

modified   lib/Driver/ToolChains.cpp
@@ -1366,6 +1366,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
                               getTriple().getArchName() +
                               ".a");
     Arguments.push_back(context.Args.MakeArgString(LibProfile));
+    Arguments.push_back(context.Args.MakeArgString("-Xlinker"));
+    Arguments.push_back(context.Args.MakeArgString("-u__llvm_profile_runtime"));
   }

   // Always add the stdlib

Ok, coverage is work now.

$ swiftenv global   local-dev
$ swiftc -o hello -profile-generate -profile-coverage-mapping hello.swift
$ ./hello
$ ls
default.profraw  hello  hello.swift
$ llvm-profdata merge -o hello.profdata default.profraw
$  llvm-cov show ./hello -instr-profile=hello.profdat\
a hello.swift
    999|    1|func test80(count: Int)-> Int{
    999|    2|    var sum = 0;
  80.9k|    3|    for i in 0...80 {
  80.9k|    4|        sum += i
    999|    5|    }
    999|    6|    return sum
    999|    7|}
    999|    8|func test20(count: Int)-> Int {
    999|    9|    var sum = 0;
  20.9k|   10|    for i in 0...20 {
  20.9k|   11|        sum += i
    999|   12|    }
    999|   13|//    print("\(sum) ")
    999|   14|    return sum
    999|   15|}
       |   16|var count20 = 20
       |   17|var count80 = 80
      1|   18|func mainmain() {
      1|   19|    var sum = 0
    999|   20|    for _ in 1..<1000 {
    999|   21|        sum += test80(count: count80)
    999|   22|        sum += test20(count: count20)
      1|   23|    }
      1|   24|//    print(sum)
      1|   25|}
       |   26|
       |   27|
       |   28|mainmain()

@belkadan
Copy link
Contributor

That seems a little suspicious to me, but I'm not familiar with this part of the code. @vedantk, any insights?

@vedantk
Copy link
Member

vedantk commented Jul 25, 2016

@belkadan On Darwin it's enough to specify the path to the profiling runtime to load in the static initializer. On Linux, you need to manufacture a use of a symbol from the runtime to load it. urugang (JIRA User)'s patch lgtm with a small change, and it matches clang's behavior. Fixed in swift/8887175d.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code coverage Area → source tooling: code coverage compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants