LegacyAPIInteropTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "OrcTestCommon.h"
  9. #include "llvm/ExecutionEngine/Orc/Legacy.h"
  10. #include "gtest/gtest.h"
  11. using namespace llvm;
  12. using namespace llvm::orc;
  13. class LegacyAPIsStandardTest : public CoreAPIsBasedStandardTest {};
  14. namespace {
  15. TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) {
  16. BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
  17. cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
  18. auto Resolver = createSymbolResolver(
  19. [&](const SymbolNameSet &Symbols) {
  20. auto FlagsMap = cantFail(JD.lookupFlags(Symbols));
  21. SymbolNameSet Result;
  22. for (auto &KV : FlagsMap)
  23. if (!KV.second.isStrong())
  24. Result.insert(KV.first);
  25. return Result;
  26. },
  27. [&](std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Symbols) {
  28. return cantFail(JD.legacyLookup(std::move(Q), Symbols));
  29. });
  30. auto RS = Resolver->getResponsibilitySet(SymbolNameSet({Bar, Baz}));
  31. EXPECT_EQ(RS.size(), 1U)
  32. << "getResponsibilitySet returned the wrong number of results";
  33. EXPECT_EQ(RS.count(Bar), 1U)
  34. << "getResponsibilitySet result incorrect. Should be {'bar'}";
  35. bool OnCompletionRun = false;
  36. auto OnCompletion = [&](Expected<SymbolMap> Result) {
  37. OnCompletionRun = true;
  38. EXPECT_TRUE(!!Result) << "Unexpected error";
  39. EXPECT_EQ(Result->size(), 2U) << "Unexpected number of resolved symbols";
  40. EXPECT_EQ(Result->count(Foo), 1U) << "Missing lookup result for foo";
  41. EXPECT_EQ(Result->count(Bar), 1U) << "Missing lookup result for bar";
  42. EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
  43. << "Incorrect address for foo";
  44. EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
  45. << "Incorrect address for bar";
  46. };
  47. auto Q = std::make_shared<AsynchronousSymbolQuery>(
  48. SymbolNameSet({Foo, Bar}), SymbolState::Resolved, OnCompletion);
  49. auto Unresolved =
  50. Resolver->lookup(std::move(Q), SymbolNameSet({Foo, Bar, Baz}));
  51. EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
  52. EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to not be resolved";
  53. EXPECT_TRUE(OnCompletionRun) << "OnCompletion was never run";
  54. }
  55. TEST_F(LegacyAPIsStandardTest, LegacyLookupHelpersFn) {
  56. bool BarMaterialized = false;
  57. BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
  58. auto LegacyLookup = [&](const std::string &Name) -> JITSymbol {
  59. if (Name == "foo")
  60. return FooSym;
  61. if (Name == "bar") {
  62. auto BarMaterializer = [&]() -> Expected<JITTargetAddress> {
  63. BarMaterialized = true;
  64. return BarAddr;
  65. };
  66. return {BarMaterializer, BarSym.getFlags()};
  67. }
  68. return nullptr;
  69. };
  70. auto RS =
  71. getResponsibilitySetWithLegacyFn(SymbolNameSet({Bar, Baz}), LegacyLookup);
  72. EXPECT_TRUE(!!RS) << "Expected getResponsibilitySetWithLegacyFn to succeed";
  73. EXPECT_EQ(RS->size(), 1U) << "Wrong number of symbols returned";
  74. EXPECT_EQ(RS->count(Bar), 1U) << "Incorrect responsibility set returned";
  75. EXPECT_FALSE(BarMaterialized)
  76. << "lookupFlags should not have materialized bar";
  77. bool OnCompletionRun = false;
  78. auto OnCompletion = [&](Expected<SymbolMap> Result) {
  79. OnCompletionRun = true;
  80. EXPECT_TRUE(!!Result) << "lookuWithLegacy failed to resolve";
  81. EXPECT_EQ(Result->size(), 2U) << "Wrong number of symbols resolved";
  82. EXPECT_EQ(Result->count(Foo), 1U) << "Result for foo missing";
  83. EXPECT_EQ(Result->count(Bar), 1U) << "Result for bar missing";
  84. EXPECT_EQ((*Result)[Foo].getAddress(), FooAddr) << "Wrong address for foo";
  85. EXPECT_EQ((*Result)[Foo].getFlags(), FooSym.getFlags())
  86. << "Wrong flags for foo";
  87. EXPECT_EQ((*Result)[Bar].getAddress(), BarAddr) << "Wrong address for bar";
  88. EXPECT_EQ((*Result)[Bar].getFlags(), BarSym.getFlags())
  89. << "Wrong flags for bar";
  90. };
  91. AsynchronousSymbolQuery Q({Foo, Bar}, SymbolState::Resolved, OnCompletion);
  92. auto Unresolved =
  93. lookupWithLegacyFn(ES, Q, SymbolNameSet({Foo, Bar, Baz}), LegacyLookup);
  94. EXPECT_TRUE(OnCompletionRun) << "OnCompletion was not run";
  95. EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
  96. EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to be unresolved";
  97. }
  98. } // namespace