已开启
feat: opt diasble importlib reflection #2047
feat: opt diasble importlib reflection #2047
已开启
duan创建于 20 天前
2 个文件变更+32-0
@@ -66,6 +66,14 @@ void SetOptimizationLevelOptions(SetFuncType setOptionHandler, const DriverOptio
66 */66 */
67void SetOptions(SetFuncType setOptionHandler, const DriverOptions& driverOptions);67void SetOptions(SetFuncType setOptionHandler, const DriverOptions& driverOptions);
68 68 
69+/**
70+ * @brief Whether the program links std.reflect (i.e. uses reflection APIs).
71+ *
72+ * @param driverOptions The data structure is obtained through parsing the compilation options.
73+ * @return true if std.reflect is among the direct/indirect built-in dependencies.
74+ */
75+bool LinksStdReflect(const DriverOptions& driverOptions);
76+ 
69/**77/**
70 * @brief Set verify opt options.78 * @brief Set verify opt options.
71 *79 *
@@ -12,7 +12,11 @@
12 12 
13#include "cangjie/Driver/ToolOptions.h"13#include "cangjie/Driver/ToolOptions.h"
14 14 
15+#include <string>
15#include <unordered_map>16#include <unordered_map>
17+#include <unordered_set>
18+ 
19+#include "cangjie/Utils/FileUtil.h"
16 20 
17namespace {21namespace {
18using namespace Cangjie;22using namespace Cangjie;
@@ -145,9 +149,29 @@ void SetVerifyOptions(SetFuncType setOptionHandler, [[maybe_unused]] const Drive
145 setOptionHandler("--only-verify-out");149 setOptionHandler("--only-verify-out");
146}150}
147 151 
152+bool LinksStdReflect(const DriverOptions& driverOptions)
153+{
154+ static const std::string reflectName = "std.reflect.cjo";
155+ auto matchDeps = [](const std::unordered_set<std::string>& deps) {
156+ for (const auto& d : deps) {
157+ if (Cangjie::FileUtil::GetFileName(d) == reflectName) {
158+ return true;
159+ }
160+ }
161+ return false;
162+ };
163+ return matchDeps(driverOptions.directBuiltinDependencies) || matchDeps(driverOptions.indirectBuiltinDependencies);
164+}
165+ 
148void SetOptions(SetFuncType setOptionHandler, const DriverOptions& driverOptions)166void SetOptions(SetFuncType setOptionHandler, const DriverOptions& driverOptions)
149{167{
150 setOptionHandler("--cangjie-pipeline");168 setOptionHandler("--cangjie-pipeline");
169+ // Skip the LTO reflection downgrade when the program uses reflection APIs, since
170+ // downgrading the reflect metadata it depends on would break them at runtime.
171+ // Programs that don't use reflection still downgrade normally.
172+ SetOptionIf(setOptionHandler,
173+ driverOptions.IsLTOEnabled() && driverOptions.disableReflection && !LinksStdReflect(driverOptions),
174+ "--cj-disable-lto-reflection");
151 SetOptionIf(setOptionHandler, driverOptions.EnableAsan(), "-cj-asan=true");175 SetOptionIf(setOptionHandler, driverOptions.EnableAsan(), "-cj-asan=true");
152 SetOptionIf(setOptionHandler, driverOptions.EnableTsan(), "-cj-tsan=true");176 SetOptionIf(setOptionHandler, driverOptions.EnableTsan(), "-cj-tsan=true");
153 SetOptionIf(setOptionHandler, driverOptions.EnableTsan(), "-tsan-instrument-atomics=false");177 SetOptionIf(setOptionHandler, driverOptions.EnableTsan(), "-tsan-instrument-atomics=false");