Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 18 | "strings" |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 22 | |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 26 | type visitDirectDepsInterface interface { |
| 27 | VisitDirectDeps(blueprint.Module, func(dep blueprint.Module)) |
| 28 | } |
| 29 | |
| 30 | func hasDirectDep(ctx visitDirectDepsInterface, m android.Module, wantDep android.Module) bool { |
| 31 | var found bool |
| 32 | ctx.VisitDirectDeps(m, func(dep blueprint.Module) { |
| 33 | if dep == wantDep { |
| 34 | found = true |
| 35 | } |
| 36 | }) |
| 37 | return found |
| 38 | } |
| 39 | |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 40 | func TestAfdoDeps(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 41 | t.Parallel() |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 42 | bp := ` |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 43 | cc_library_shared { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 44 | name: "libTest", |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 45 | srcs: ["test.c"], |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 46 | static_libs: ["libFoo"], |
| 47 | afdo: true, |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 48 | lto: { |
| 49 | thin: true, |
| 50 | }, |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 53 | cc_library_static { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 54 | name: "libFoo", |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 55 | srcs: ["foo.c"], |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 56 | static_libs: ["libBar"], |
| 57 | } |
| 58 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 59 | cc_library_static { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 60 | name: "libBar", |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 61 | srcs: ["bar.c"], |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 62 | } |
| 63 | ` |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 64 | |
| 65 | result := android.GroupFixturePreparers( |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 66 | PrepareForTestWithFdoProfile, |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 67 | prepareForCcTest, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 68 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), |
| 69 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 70 | variables.AfdoProfiles = []string{ |
| 71 | "libTest://afdo_profiles_package:libTest_afdo", |
| 72 | } |
| 73 | }), |
| 74 | android.MockFS{ |
| 75 | "afdo_profiles_package/Android.bp": []byte(` |
| 76 | fdo_profile { |
| 77 | name: "libTest_afdo", |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 78 | arch: { |
| 79 | arm64: { |
| 80 | profile: "libTest.afdo", |
| 81 | }, |
| 82 | }, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 83 | } |
| 84 | `), |
| 85 | }.AddToFixture(), |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 86 | ).RunTestWithBp(t, bp) |
| 87 | |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 88 | profileSampleCFlag := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo" |
| 89 | uniqueInternalLinkageNamesCFlag := "-funique-internal-linkage-names" |
| 90 | afdoLtoLdFlag := "-Wl,-plugin-opt,-import-instr-limit=40" |
| 91 | noAfdoLtoLdFlag := "-Wl,-plugin-opt,-import-instr-limit=5" |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 92 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 93 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") |
| 94 | libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") |
| 95 | libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest") |
| 96 | |
| 97 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps |
| 98 | cFlags := libTest.Rule("cc").Args["cFlags"] |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 99 | if !strings.Contains(cFlags, profileSampleCFlag) { |
| 100 | t.Errorf("Expected 'libTest' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
| 101 | } |
| 102 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 103 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
| 104 | } |
| 105 | |
| 106 | ldFlags := libTest.Rule("ld").Args["ldFlags"] |
| 107 | if !strings.Contains(ldFlags, afdoLtoLdFlag) { |
| 108 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in ldflags %q", afdoLtoLdFlag, ldFlags) |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"] |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 112 | if !strings.Contains(cFlags, profileSampleCFlag) { |
| 113 | t.Errorf("Expected 'libFooAfdoVariant' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
| 114 | } |
| 115 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 116 | t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"] |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 120 | if !strings.Contains(cFlags, profileSampleCFlag) { |
| 121 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
| 122 | } |
| 123 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 124 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags) |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Check dependency edge from afdo-enabled module to static deps |
| 128 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 129 | t.Errorf("libTest missing dependency on afdo variant of libFoo") |
| 130 | } |
| 131 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 132 | if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 133 | t.Errorf("libTest missing dependency on afdo variant of libBar") |
| 134 | } |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 135 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 136 | // Verify non-afdo variant exists and doesn't contain afdo |
| 137 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") |
| 138 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static") |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 139 | |
| 140 | cFlags = libFoo.Rule("cc").Args["cFlags"] |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 141 | if strings.Contains(cFlags, profileSampleCFlag) { |
| 142 | t.Errorf("Expected 'libFoo' to not enable afdo profile, but found %q in cflags %q", profileSampleCFlag, cFlags) |
| 143 | } |
| 144 | if strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 145 | t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags) |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 146 | } |
| 147 | cFlags = libBar.Rule("cc").Args["cFlags"] |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 148 | if strings.Contains(cFlags, profileSampleCFlag) { |
| 149 | t.Errorf("Expected 'libBar' to not enable afdo profile, but found %q in cflags %q", profileSampleCFlag, cFlags) |
| 150 | } |
| 151 | if strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 152 | t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags) |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 155 | // Check dependency edges of static deps |
| 156 | if hasDirectDep(result, libTest.Module(), libFoo.Module()) { |
| 157 | t.Errorf("libTest should not depend on non-afdo variant of libFoo") |
| 158 | } |
| 159 | |
| 160 | if !hasDirectDep(result, libFoo.Module(), libBar.Module()) { |
| 161 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 162 | } |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 163 | |
| 164 | // Verify that the arm variant does not have FDO since the fdo_profile module only has a profile for arm64 |
| 165 | libTest32 := result.ModuleForTests("libTest", "android_arm_armv7-a-neon_shared") |
| 166 | libFooAfdoVariant32 := result.ModuleForTests("libFoo", "android_arm_armv7-a-neon_static_afdo-libTest_lto-thin") |
| 167 | libBarAfdoVariant32 := result.ModuleForTests("libBar", "android_arm_armv7-a-neon_static_afdo-libTest_lto-thin") |
| 168 | |
| 169 | cFlags = libTest32.Rule("cc").Args["cFlags"] |
| 170 | if strings.Contains(cFlags, profileSampleCFlag) { |
| 171 | t.Errorf("Expected arm32 'libTest' not to enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags) |
| 172 | } |
| 173 | |
| 174 | // TODO(b/324141705): when the fdo_profile module doesn't provide a source file the dependencies don't get |
| 175 | // -funique-internal-linkage-names but the module does. |
| 176 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 177 | t.Errorf("Expected arm32 'libTest' to enable -funique-internal-linkage-names but did not find %q in cflags %q", |
| 178 | uniqueInternalLinkageNamesCFlag, cFlags) |
| 179 | } |
| 180 | |
| 181 | ldFlags = libTest32.Rule("ld").Args["ldFlags"] |
| 182 | if !strings.Contains(ldFlags, noAfdoLtoLdFlag) { |
| 183 | t.Errorf("Expected arm32 'libTest' to not enable afdo, but did not find %q in ldflags %q", noAfdoLtoLdFlag, ldFlags) |
| 184 | } |
| 185 | if strings.Contains(ldFlags, afdoLtoLdFlag) { |
| 186 | t.Errorf("Expected arm32 'libTest' to not enable afdo, but found %q in ldflags %q", afdoLtoLdFlag, ldFlags) |
| 187 | } |
| 188 | |
| 189 | // Check dependency edge from afdo-enabled module to static deps |
| 190 | if !hasDirectDep(result, libTest32.Module(), libFooAfdoVariant32.Module()) { |
| 191 | t.Errorf("arm32 libTest missing dependency on afdo variant of libFoo") |
| 192 | } |
| 193 | |
| 194 | if !hasDirectDep(result, libFooAfdoVariant32.Module(), libBarAfdoVariant32.Module()) { |
| 195 | t.Errorf("arm32 libTest missing dependency on afdo variant of libBar") |
| 196 | } |
| 197 | |
| 198 | cFlags = libFooAfdoVariant32.Rule("cc").Args["cFlags"] |
| 199 | if strings.Contains(cFlags, profileSampleCFlag) { |
| 200 | t.Errorf("Expected arm32 'libFoo' to not enable afdo profile, but found %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags) |
| 201 | } |
| 202 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 203 | t.Errorf("Expected arm32 'libFoo' to enable afdo, but did not find %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags) |
| 204 | } |
| 205 | cFlags = libBarAfdoVariant32.Rule("cc").Args["cFlags"] |
| 206 | if strings.Contains(cFlags, profileSampleCFlag) { |
| 207 | t.Errorf("Expected arm32 'libBar' to not enable afdo profile, but found %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags) |
| 208 | } |
| 209 | if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) { |
| 210 | t.Errorf("Expected arm32 'libBar' to enable afdo, but did not find %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags) |
| 211 | } |
| 212 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | func TestAfdoEnabledOnStaticDepNoAfdo(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 216 | t.Parallel() |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 217 | bp := ` |
| 218 | cc_library_shared { |
| 219 | name: "libTest", |
| 220 | srcs: ["foo.c"], |
| 221 | static_libs: ["libFoo"], |
| 222 | } |
| 223 | |
| 224 | cc_library_static { |
| 225 | name: "libFoo", |
| 226 | srcs: ["foo.c"], |
| 227 | static_libs: ["libBar"], |
| 228 | afdo: true, // TODO(b/256670524): remove support for enabling afdo from static only libraries, this can only propagate from shared libraries/binaries |
| 229 | } |
| 230 | |
| 231 | cc_library_static { |
| 232 | name: "libBar", |
| 233 | } |
| 234 | ` |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 235 | |
| 236 | result := android.GroupFixturePreparers( |
| 237 | prepareForCcTest, |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 238 | PrepareForTestWithFdoProfile, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 239 | android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libFoo.afdo", ""), |
| 240 | android.MockFS{ |
| 241 | "afdo_profiles_package/Android.bp": []byte(` |
| 242 | soong_namespace { |
| 243 | } |
| 244 | fdo_profile { |
| 245 | name: "libFoo_afdo", |
| 246 | profile: "libFoo.afdo", |
| 247 | } |
| 248 | `), |
| 249 | }.AddToFixture(), |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 250 | ).RunTestWithBp(t, bp) |
| 251 | |
| 252 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared").Module() |
| 253 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") |
| 254 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static").Module() |
| 255 | |
| 256 | if !hasDirectDep(result, libTest, libFoo.Module()) { |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 257 | t.Errorf("libTest missing dependency on non-afdo variant of libFoo") |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if !hasDirectDep(result, libFoo.Module(), libBar) { |
Colin Cross | da4c89f | 2024-02-07 15:03:01 -0800 | [diff] [blame^] | 261 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | fooVariants := result.ModuleVariantsForTests("foo") |
| 265 | for _, v := range fooVariants { |
| 266 | if strings.Contains(v, "afdo-") { |
| 267 | t.Errorf("Expected no afdo variant of 'foo', got %q", v) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | cFlags := libFoo.Rule("cc").Args["cFlags"] |
| 272 | if w := "-fprofile-sample-accurate"; strings.Contains(cFlags, w) { |
| 273 | t.Errorf("Expected 'foo' to not enable afdo, but found %q in cflags %q", w, cFlags) |
| 274 | } |
| 275 | |
| 276 | barVariants := result.ModuleVariantsForTests("bar") |
| 277 | for _, v := range barVariants { |
| 278 | if strings.Contains(v, "afdo-") { |
| 279 | t.Errorf("Expected no afdo variant of 'bar', got %q", v) |
| 280 | } |
| 281 | } |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 282 | } |
Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 283 | |
| 284 | func TestAfdoEnabledWithRuntimeDepNoAfdo(t *testing.T) { |
| 285 | bp := ` |
| 286 | cc_library { |
| 287 | name: "libTest", |
| 288 | srcs: ["foo.c"], |
| 289 | runtime_libs: ["libFoo"], |
| 290 | afdo: true, |
| 291 | } |
| 292 | |
| 293 | cc_library { |
| 294 | name: "libFoo", |
| 295 | } |
| 296 | ` |
Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 297 | |
| 298 | result := android.GroupFixturePreparers( |
| 299 | prepareForCcTest, |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 300 | PrepareForTestWithFdoProfile, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 301 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), |
| 302 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 303 | variables.AfdoProfiles = []string{ |
| 304 | "libTest://afdo_profiles_package:libTest_afdo", |
| 305 | } |
| 306 | }), |
| 307 | android.MockFS{ |
| 308 | "afdo_profiles_package/Android.bp": []byte(` |
| 309 | fdo_profile { |
| 310 | name: "libTest_afdo", |
| 311 | profile: "libTest.afdo", |
| 312 | } |
| 313 | `), |
| 314 | }.AddToFixture(), |
Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 315 | ).RunTestWithBp(t, bp) |
| 316 | |
| 317 | libFooVariants := result.ModuleVariantsForTests("libFoo") |
| 318 | for _, v := range libFooVariants { |
| 319 | if strings.Contains(v, "afdo-") { |
| 320 | t.Errorf("Expected no afdo variant of 'foo', got %q", v) |
| 321 | } |
| 322 | } |
| 323 | } |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 324 | |
| 325 | func TestAfdoEnabledWithMultiArchs(t *testing.T) { |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 326 | bp := ` |
| 327 | cc_library_shared { |
| 328 | name: "foo", |
| 329 | srcs: ["test.c"], |
| 330 | afdo: true, |
| 331 | compile_multilib: "both", |
| 332 | } |
| 333 | ` |
| 334 | result := android.GroupFixturePreparers( |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 335 | PrepareForTestWithFdoProfile, |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 336 | prepareForCcTest, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 337 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm.afdo", ""), |
| 338 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm64.afdo", ""), |
| 339 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 340 | variables.AfdoProfiles = []string{ |
| 341 | "foo://afdo_profiles_package:foo_afdo", |
| 342 | } |
| 343 | }), |
| 344 | android.MockFS{ |
| 345 | "afdo_profiles_package/Android.bp": []byte(` |
| 346 | soong_namespace { |
| 347 | } |
| 348 | fdo_profile { |
| 349 | name: "foo_afdo", |
| 350 | arch: { |
| 351 | arm: { |
| 352 | profile: "foo_arm.afdo", |
| 353 | }, |
| 354 | arm64: { |
| 355 | profile: "foo_arm64.afdo", |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | `), |
| 360 | }.AddToFixture(), |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 361 | ).RunTestWithBp(t, bp) |
| 362 | |
| 363 | fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared") |
| 364 | fooArmCFlags := fooArm.Rule("cc").Args["cFlags"] |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 365 | if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm.afdo"; !strings.Contains(fooArmCFlags, w) { |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 366 | t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArmCFlags) |
| 367 | } |
| 368 | |
| 369 | fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a_shared") |
| 370 | fooArm64CFlags := fooArm64.Rule("cc").Args["cFlags"] |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 371 | if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm64.afdo"; !strings.Contains(fooArm64CFlags, w) { |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 372 | t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArm64CFlags) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | func TestMultipleAfdoRDeps(t *testing.T) { |
| 377 | t.Parallel() |
| 378 | bp := ` |
| 379 | cc_library_shared { |
| 380 | name: "libTest", |
| 381 | srcs: ["test.c"], |
| 382 | static_libs: ["libFoo"], |
| 383 | afdo: true, |
| 384 | } |
| 385 | |
| 386 | cc_library_shared { |
| 387 | name: "libBar", |
| 388 | srcs: ["bar.c"], |
| 389 | static_libs: ["libFoo"], |
| 390 | afdo: true, |
| 391 | } |
| 392 | |
| 393 | cc_library_static { |
| 394 | name: "libFoo", |
| 395 | srcs: ["foo.c"], |
| 396 | } |
| 397 | ` |
| 398 | |
| 399 | result := android.GroupFixturePreparers( |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 400 | PrepareForTestWithFdoProfile, |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 401 | prepareForCcTest, |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 402 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), |
| 403 | android.FixtureAddTextFile("afdo_profiles_package/libBar.afdo", ""), |
| 404 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 405 | variables.AfdoProfiles = []string{ |
| 406 | "libTest://afdo_profiles_package:libTest_afdo", |
| 407 | "libBar://afdo_profiles_package:libBar_afdo", |
| 408 | } |
| 409 | }), |
| 410 | android.MockFS{ |
| 411 | "afdo_profiles_package/Android.bp": []byte(` |
| 412 | fdo_profile { |
| 413 | name: "libTest_afdo", |
| 414 | profile: "libTest.afdo", |
| 415 | } |
| 416 | fdo_profile { |
| 417 | name: "libBar_afdo", |
| 418 | profile: "libBar.afdo", |
| 419 | } |
| 420 | `), |
| 421 | }.AddToFixture(), |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 422 | ).RunTestWithBp(t, bp) |
| 423 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 424 | expectedCFlagLibTest := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo" |
| 425 | expectedCFlagLibBar := "-fprofile-sample-use=afdo_profiles_package/libBar.afdo" |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 426 | |
| 427 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 428 | libFooAfdoVariantWithLibTest := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 429 | |
| 430 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_shared") |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 431 | libFooAfdoVariantWithLibBar := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libBar") |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 432 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 433 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 434 | cFlags := libTest.Rule("cc").Args["cFlags"] |
| 435 | if !strings.Contains(cFlags, expectedCFlagLibTest) { |
| 436 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags) |
| 437 | } |
| 438 | cFlags = libBar.Rule("cc").Args["cFlags"] |
| 439 | if !strings.Contains(cFlags, expectedCFlagLibBar) { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 440 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags) |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 443 | cFlags = libFooAfdoVariantWithLibTest.Rule("cc").Args["cFlags"] |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 444 | if !strings.Contains(cFlags, expectedCFlagLibTest) { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 445 | t.Errorf("Expected 'libFooAfdoVariantWithLibTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags) |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 448 | cFlags = libFooAfdoVariantWithLibBar.Rule("cc").Args["cFlags"] |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 449 | if !strings.Contains(cFlags, expectedCFlagLibBar) { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 450 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags) |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // Check dependency edges of static deps |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 454 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariantWithLibTest.Module()) { |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 455 | t.Errorf("libTest missing dependency on afdo variant of libFoo") |
| 456 | } |
| 457 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 458 | if !hasDirectDep(result, libBar.Module(), libFooAfdoVariantWithLibBar.Module()) { |
| 459 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") |
Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 460 | } |
| 461 | } |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 462 | |
| 463 | func TestAfdoDepsWithoutProfile(t *testing.T) { |
| 464 | t.Parallel() |
| 465 | bp := ` |
| 466 | cc_library_shared { |
| 467 | name: "libTest", |
| 468 | srcs: ["test.c"], |
| 469 | static_libs: ["libFoo"], |
| 470 | afdo: true, |
| 471 | } |
| 472 | |
| 473 | cc_library_static { |
| 474 | name: "libFoo", |
| 475 | srcs: ["foo.c"], |
| 476 | static_libs: ["libBar"], |
| 477 | } |
| 478 | |
| 479 | cc_library_static { |
| 480 | name: "libBar", |
| 481 | srcs: ["bar.c"], |
| 482 | } |
| 483 | ` |
| 484 | |
| 485 | result := android.GroupFixturePreparers( |
| 486 | PrepareForTestWithFdoProfile, |
| 487 | prepareForCcTest, |
| 488 | ).RunTestWithBp(t, bp) |
| 489 | |
| 490 | // Even without a profile path, the afdo enabled libraries should be built with |
| 491 | // -funique-internal-linkage-names. |
| 492 | expectedCFlag := "-funique-internal-linkage-names" |
| 493 | |
| 494 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") |
| 495 | libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") |
| 496 | libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest") |
| 497 | |
| 498 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps |
| 499 | cFlags := libTest.Rule("cc").Args["cFlags"] |
| 500 | if !strings.Contains(cFlags, expectedCFlag) { |
| 501 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) |
| 502 | } |
| 503 | |
| 504 | cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"] |
| 505 | if !strings.Contains(cFlags, expectedCFlag) { |
| 506 | t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) |
| 507 | } |
| 508 | |
| 509 | cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"] |
| 510 | if !strings.Contains(cFlags, expectedCFlag) { |
| 511 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) |
| 512 | } |
| 513 | // Check dependency edge from afdo-enabled module to static deps |
| 514 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) { |
| 515 | t.Errorf("libTest missing dependency on afdo variant of libFoo") |
| 516 | } |
| 517 | |
| 518 | if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) { |
| 519 | t.Errorf("libTest missing dependency on afdo variant of libBar") |
| 520 | } |
| 521 | |
| 522 | // Verify non-afdo variant exists and doesn't contain afdo |
| 523 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") |
| 524 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static") |
| 525 | |
| 526 | cFlags = libFoo.Rule("cc").Args["cFlags"] |
| 527 | if strings.Contains(cFlags, expectedCFlag) { |
| 528 | t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) |
| 529 | } |
| 530 | cFlags = libBar.Rule("cc").Args["cFlags"] |
| 531 | if strings.Contains(cFlags, expectedCFlag) { |
| 532 | t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) |
| 533 | } |
| 534 | |
| 535 | // Check dependency edges of static deps |
| 536 | if hasDirectDep(result, libTest.Module(), libFoo.Module()) { |
| 537 | t.Errorf("libTest should not depend on non-afdo variant of libFoo") |
| 538 | } |
| 539 | |
| 540 | if !hasDirectDep(result, libFoo.Module(), libBar.Module()) { |
| 541 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") |
| 542 | } |
| 543 | } |