Skip to content

Commit e5869c7

Browse files
committed
LLVM_BITCODE_GENERATION_FLAGS feature.
1 parent a25ce9c commit e5869c7

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ To install, simply do (making sure to include those `...`)
6464
go get github.com/SRI-CSL/gllvm/cmd/...
6565
```
6666
This should install four binaries: `gclang`, `gclang++`, `get-bc`, and `gsanity-check`
67-
in the `$GOPATH/bin` directory.
67+
in the `$GOPATH/bin` directory.
6868

6969
## Usage
7070

@@ -236,6 +236,14 @@ environment variables. For more details of what's under the `gllvm` hood, try
236236
gsanity-check -e
237237
```
238238

239+
## Customizing the BitCode Generation (e.g. LTO)
240+
241+
In some situations it is desirable to pass certain flags to `clang` in the step that
242+
produces the bitcode. This can be fulfilled by setting the
243+
`LLVM_BITCODE_GENERATION_FLAGS` environment variable to the desired
244+
flags, for example `"-flto -fwhole-program-vtables"`.
245+
246+
239247
## License
240248

241249
`gllvm` is released under a BSD license. See the file `LICENSE` for [details.](LICENSE)

shared/compiler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ func buildObjectFile(compilerExecName string, pr parserResult, srcFile string, o
241241
// Tries to build the specified source file to bitcode
242242
func buildBitcodeFile(compilerExecName string, pr parserResult, srcFile string, bcFile string) (success bool) {
243243
args := pr.CompileArgs[:]
244+
//iam: 03/24/2020 extend with the LLVM_BITCODE_GENERATION_FLAGS if any.
245+
args = append(args, LLVMbcGen...)
244246
args = append(args, "-emit-llvm", "-c", srcFile, "-o", bcFile)
245247
success, err := execCmd(compilerExecName, args, "")
246248
if !success {

shared/constants.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ package shared
4848
// 1.2.4 June 21 2019 Random fixes (basically the same as wllvm 1.2.7)
4949
//
5050
// 1.2.5 October 23 2019 Fixes for issues #30 and #31. Plus a new branch where I can futz hygenically.
51+
//
52+
// 1.2.6 March 24 2020 Added the support for the LLVM_BITCODE_GENERATION_FLAGS environment variable.
53+
// See https://github.com/travitch/whole-program-llvm/issues/96 for details.
54+
//
5155

52-
const gllvmVersion = "1.2.5"
53-
const gllvmReleaseDate = "October 23 2019"
56+
const gllvmVersion = "1.2.6"
57+
const gllvmReleaseDate = "March 24 2020"
5458

5559
const osDARWIN = "darwin"
5660
const osLINUX = "linux"

shared/environment.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ package shared
3535

3636
import (
3737
"os"
38+
"strings"
3839
)
3940

4041
const (
@@ -82,6 +83,9 @@ var LLVMObjcopy string
8283
//LLVMLd is the path to the ld executable used to attach the bitcode on OSX.
8384
var LLVMLd string
8485

86+
//LLVMbcGen is the list of args to pass to clang during the bitcode generation step.
87+
var LLVMbcGen []string
88+
8589
const (
8690
envpath = "LLVM_COMPILER_PATH"
8791
envcc = "LLVM_CC_NAME"
@@ -95,14 +99,17 @@ const (
9599
envld = "GLLVM_LD" //iam: we are deviating from wllvm here.
96100
envobjcopy = "GLLVM_OBJCOPY" //iam: we are deviating from wllvm here.
97101
//wllvm uses a BINUTILS_TARGET_PREFIX, which seems less general.
102+
//iam: 03/24/2020 new feature to pass things like "-flto -fwhole-program-vtables"
103+
// to clang during the bitcode generation step
104+
envbcgen = "LLVM_BITCODE_GENERATION_FLAGS"
98105
)
99106

100107
func init() {
101108
FetchEnvironment()
102109
}
103110

104-
func printEnvironment() {
105-
vars := []string{envpath, envcc, envcxx, envar, envlnk, envcfg, envbc, envlvl, envfile, envobjcopy, envld}
111+
func PrintEnvironment() {
112+
vars := []string{envpath, envcc, envcxx, envar, envlnk, envcfg, envbc, envlvl, envfile, envobjcopy, envld, envbcgen}
106113

107114
informUser("\nLiving in this environment:\n\n")
108115
for _, v := range vars {
@@ -116,7 +123,23 @@ func printEnvironment() {
116123

117124
}
118125

119-
// used in testing
126+
// also used in testing
127+
func ResetEnvironment() {
128+
LLVMToolChainBinDir = ""
129+
LLVMCCName = ""
130+
LLVMCXXName = ""
131+
LLVMARName = ""
132+
LLVMLINKName = ""
133+
LLVMConfigureOnly = ""
134+
LLVMBitcodeStorePath = ""
135+
LLVMLoggingLevel = ""
136+
LLVMLoggingFile = ""
137+
LLVMObjcopy = ""
138+
LLVMLd = ""
139+
LLVMbcGen = []string{}
140+
}
141+
142+
// also used in testing
120143
func FetchEnvironment() {
121144
LLVMToolChainBinDir = os.Getenv(envpath)
122145
LLVMCCName = os.Getenv(envcc)
@@ -132,4 +155,7 @@ func FetchEnvironment() {
132155

133156
LLVMObjcopy = os.Getenv(envobjcopy)
134157
LLVMLd = os.Getenv(envld)
158+
159+
LLVMbcGen = strings.Fields(os.Getenv(envbcgen))
160+
135161
}

shared/sanity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func SanityCheck() {
108108
informUser("\nVersion info: gsanity-check version %v\nReleased: %v\n", gllvmVersion, gllvmReleaseDate)
109109

110110
if sa.Environment {
111-
printEnvironment()
111+
PrintEnvironment()
112112
}
113113

114114
checkLogging()

tests/env_and_arg_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func Test_env_and_args(t *testing.T) {
4141

4242
args := []string{"get-bc", "-v", "../data/hello"}
4343

44+
if verbose {
45+
shared.PrintEnvironment()
46+
}
47+
48+
49+
shared.ResetEnvironment()
50+
4451
ea := shared.ParseSwitches(args)
4552
if !ea.Verbose {
4653
t.Errorf("ParseSwitches: -v flag not working\n")
@@ -52,6 +59,7 @@ func Test_env_and_args(t *testing.T) {
5259
t.Errorf("ParseSwitches: InputFile incorrect: %v\n", ea.InputFile)
5360
}
5461

62+
//iam: this test assumes LLVMToolChainBinDir = ""
5563
checkExecutables(t, ea, "llvm-link", "llvm-ar", "ar", "clang", "clang++")
5664

5765
os.Setenv("LLVM_COMPILER_PATH", "/the_future_is_here")
@@ -81,5 +89,4 @@ func Test_env_and_args(t *testing.T) {
8189
"ar",
8290
"/the_future_is_here/clang-666",
8391
"/the_future_is_here/clang++-666")
84-
8592
}

0 commit comments

Comments
 (0)