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

Private/vromanov/module exp #7

Open
wants to merge 2 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Lex/HeaderSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class HeaderSearch {
/// The hash used for module cache paths.
std::string ModuleHash;

std::string ContextModuleHash;

/// The path to the module cache.
std::string ModuleCachePath;

Expand Down Expand Up @@ -406,6 +408,8 @@ class HeaderSearch {
/// Set the hash to use for module cache paths.
void setModuleHash(StringRef Hash) { ModuleHash = std::string(Hash); }

void setContextModuleHash(StringRef Hash) { ContextModuleHash = std::string(Hash); }

/// Set the path to the module cache.
void setModuleCachePath(StringRef CachePath) {
ModuleCachePath = std::string(CachePath);
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5235,6 +5235,25 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(
Args.MakeArgString(Twine("-fsycl-unique-prefix=") + UniqueID));

if (getenv("USE_MODULE")) {
SmallString<128> CachePath;
const bool HasPath = Driver::getDefaultModuleCachePath(CachePath);
if (HasPath) {
SmallString<128> BaseDir(C.getDriver().Dir);
llvm::sys::path::append(BaseDir, "..", "include");
llvm::sys::path::append(BaseDir, "module.modulemap");

CmdArgs.push_back("-fmodules"); // VLAD
//
CmdArgs.push_back(
Args.MakeArgString(Twine("-fmodules-cache-path=") + CachePath));

CmdArgs.push_back(
Args.MakeArgString(Twine("-fmodule-map-file=") + BaseDir));
CmdArgs.push_back("-fmodules-validate-system-headers");
}
}

// Disable parallel for range-rounding for anything involving FPGA
auto SYCLTCRange = C.getOffloadToolChains<Action::OFK_SYCL>();
bool HasFPGA = false;
Expand Down
168 changes: 168 additions & 0 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <optional>
#include <time.h>
#include <utility>
#include <sstream>

using namespace clang;

Expand Down Expand Up @@ -497,6 +498,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {

if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
std::string ModuleHash = getInvocation().getModuleHash();
/* printf("VLAD Hash Orig= %s\n", ModuleHash.c_str()); */
PP->getHeaderSearchInfo().setModuleHash(ModuleHash);
PP->getHeaderSearchInfo().setModuleCachePath(
getSpecificModuleCachePath(ModuleHash));
Expand Down Expand Up @@ -1731,6 +1733,19 @@ bool CompilerInstance::loadModuleFile(StringRef FileName) {
auto &ListenerRef = *Listener;
ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
std::move(Listener));
getInvocation().getModuleHash();

#if 0
if (auto FilteredMacros = llvm::make_filter_range(
this->getPreprocessor().macros(),
[](const auto &Macro) { return true || Macro.first->isFromAST(); });
!FilteredMacros.empty()) {
printf(" Macro Definitions:\n");
for (/*<IdentifierInfo *, MacroState> pair*/ const auto &Macro :
FilteredMacros)
printf("%s\n", Macro.first->getName().str().c_str());
}
#endif

// Try to load the module file.
switch (TheASTReader->ReadAST(
Expand Down Expand Up @@ -1780,6 +1795,7 @@ static ModuleSource selectModuleSource(
ModuleFilename = BuiltModuleIt->second;
return MS_ModuleBuildPragma;
}
// VLAD

// Try to load the module from the prebuilt module path.
const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
Expand All @@ -1801,6 +1817,138 @@ static ModuleSource selectModuleSource(
return MS_ModuleNotFound;
}

static std::string getModuleHash(CompilerInvocation &CInvok,
CompilerInstance &CInst) {
// FIXME: Consider using SHA1 instead of MD5.
llvm::HashBuilder<llvm::MD5, llvm::support::endianness::native> HBuilder;

// Note: For QoI reasons, the things we use as a hash here should all be
// dumped via the -module-info flag.

// Start the signature with the compiler version.
HBuilder.add(getClangFullRepositoryVersion());

// Also include the serialization version, in case LLVM_APPEND_VC_REV is off
// and getClangFullRepositoryVersion() doesn't include git revision.
HBuilder.add(serialization::VERSION_MAJOR, serialization::VERSION_MINOR);

// Extend the signature with the language options
#define LANGOPT(Name, Bits, Default, Description) HBuilder.add(CInvok.LangOpts->Name);
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
HBuilder.add(static_cast<unsigned>(CInvok.LangOpts->get##Name()));
#define BENIGN_LANGOPT(Name, Bits, Default, Description)
#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
#include "clang/Basic/LangOptions.def"

HBuilder.addRange(CInvok.LangOpts->ModuleFeatures);

HBuilder.add(CInvok.LangOpts->ObjCRuntime);
HBuilder.addRange(CInvok.LangOpts->CommentOpts.BlockCommandNames);

// Extend the signature with the target options.
HBuilder.add(CInvok.TargetOpts->Triple, CInvok.TargetOpts->CPU, CInvok.TargetOpts->TuneCPU,
CInvok.TargetOpts->ABI);
HBuilder.addRange(CInvok.TargetOpts->FeaturesAsWritten);

// Extend the signature with preprocessor options.
const PreprocessorOptions &ppOpts = CInvok.getPreprocessorOpts();
HBuilder.add(ppOpts.UsePredefines, ppOpts.DetailedRecord);

const HeaderSearchOptions &hsOpts = CInvok.getHeaderSearchOpts();
for (const auto &Macro : CInvok.getPreprocessorOpts().Macros) {
// If we're supposed to ignore this macro for the purposes of modules,
// don't put it into the hash.
/* fprintf(stderr, "VLAD MacroDef = %s\n", Macro.first.c_str()); */
if (!hsOpts.ModulesIgnoreMacros.empty()) {
// Check whether we're ignoring this macro.
StringRef MacroDef = Macro.first;
if (hsOpts.ModulesIgnoreMacros.count(
llvm::CachedHashString(MacroDef.split('=').first)))
continue;
}

HBuilder.add(Macro);
}

for (IdentifierTable::iterator
Id = CInst.getPreprocessor().getIdentifierTable().begin(),
IdEnd = CInst.getPreprocessor().getIdentifierTable().end();
Id != IdEnd; ++Id) {

auto Name = Id->second->getName();

/* fprintf(stderr, "VLAD MacroDef = %s\n", Name.str().c_str()); */

if (Id->second->hadMacroDefinition()) {
/* fprintf(stderr, "hadMacroDefinition VLAD MacroDef = %s\n", Name.str().c_str()); */
MacroDefinition Macro =
CInst.getPreprocessor().getMacroDefinition(Id->second);

MacroInfo *MacroInfoVal = Macro.getMacroInfo();
/* MacroInfoVal->dump(); */
/* fprintf(stderr, "\n"); */

std::stringstream MacroDef;
SmallString<128> SpellingBuffer;
for (const auto &T : MacroInfoVal->tokens()) {
if (T.hasLeadingSpace())
MacroDef << ' ';

MacroDef << CInst.getPreprocessor().getSpelling(T, SpellingBuffer).str();
}
/* fprintf(stderr, "MacroDef = %s\n", MacroDef.str().c_str()); */

HBuilder.add(Name);
HBuilder.add(MacroDef.str());

}
}

// Extend the signature with the sysroot and other header search options.
HBuilder.add(hsOpts.Sysroot, hsOpts.ModuleFormat, hsOpts.UseDebugInfo,
hsOpts.UseBuiltinIncludes, hsOpts.UseStandardSystemIncludes,
hsOpts.UseStandardCXXIncludes, hsOpts.UseLibcxx,
hsOpts.ModulesValidateDiagnosticOptions);
HBuilder.add(hsOpts.ResourceDir);

if (hsOpts.ModulesStrictContextHash) {
HBuilder.addRange(hsOpts.SystemHeaderPrefixes);
HBuilder.addRange(hsOpts.UserEntries);

const DiagnosticOptions &diagOpts = CInvok.getDiagnosticOpts();
#define DIAGOPT(Name, Bits, Default) HBuilder.add(diagOpts.Name);
#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
HBuilder.add(diagOpts.get##Name());
#include "clang/Basic/DiagnosticOptions.def"
#undef DIAGOPT
#undef ENUM_DIAGOPT
}

// Extend the signature with the user build path.
HBuilder.add(hsOpts.ModuleUserBuildPath);

// Extend the signature with the module file extensions.
for (const auto &ext : CInvok.getFrontendOpts().ModuleFileExtensions)
ext->hashExtension(HBuilder);

// When compiling with -gmodules, also hash -fdebug-prefix-map as it
// affects the debug info in the PCM.
if (CInvok.getCodeGenOpts().DebugTypeExtRefs)
HBuilder.addRange(CInvok.getCodeGenOpts().DebugPrefixMap);

// Extend the signature with the enabled sanitizers, if at least one is
// enabled. Sanitizers which cannot affect AST generation aren't hashed.
SanitizerSet SanHash = CInvok.LangOpts->Sanitize;
SanHash.clear(getPPTransparentSanitizers());
if (!SanHash.empty())
HBuilder.add(SanHash.Mask);

llvm::MD5::MD5Result Result;
HBuilder.getHasher().final(Result);
uint64_t Hash = Result.high() ^ Result.low();
return toString(llvm::APInt(64, Hash), 36, /*Signed=*/false);
}

ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
StringRef ModuleName, SourceLocation ImportLoc,
SourceLocation ModuleNameLoc, bool IsInclusionDirective) {
Expand All @@ -1811,6 +1959,22 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(

// Select the source and filename for loading the named module.
std::string ModuleFilename;
/* printf("VLAD Before loading\n"); */
/* std::string ModuleHash = getModuleHash(getInvocation(), *this); */

/* printf("VLAD Hash Context = %s\n", ModuleHash.c_str()); */

/* HS.setContextModuleHash(ModuleHash); */

/* std::string OldHash = HS.getModuleHash().str(); */
/* std::string OldModuleCachePath = HS.getModuleCachePath().str(); */
/* HS.setModuleHash(ModuleHash); */

/* SmallString<256> ParentPath = */
/* llvm::sys::path::parent_path(OldModuleCachePath); */
/* llvm::sys::path::append(ParentPath, ModuleHash); */
/* HS.setModuleCachePath(ParentPath); */

ModuleSource Source =
selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
if (Source == MS_ModuleNotFound) {
Expand Down Expand Up @@ -1956,6 +2120,10 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
return nullptr;
}

//RESOTRE
/* HS.setModuleHash(OldHash); */
/* HS.setModuleCachePath(OldModuleCachePath); */

// Okay, we've rebuilt and now loaded the module.
return M;
}
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,7 @@ std::string CompilerInvocation::getModuleHash() const {
for (const auto &Macro : getPreprocessorOpts().Macros) {
// If we're supposed to ignore this macro for the purposes of modules,
// don't put it into the hash.
/* fprintf(stderr, "VLAD MacroDef = %s\n", Macro.first.c_str()); */
if (!hsOpts.ModulesIgnoreMacros.empty()) {
// Check whether we're ignoring this macro.
StringRef MacroDef = Macro.first;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Lex/HeaderSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,

SmallString<128> HashStr;
llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
llvm::sys::path::append(Result, ModuleName + "-" + HashStr + "-" +
ContextModuleHash + ".pcm");
}
return Result.str().str();
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,7 @@ void ASTStmtReader::VisitSYCLBuiltinFieldTypeExpr(SYCLBuiltinFieldTypeExpr *E) {
E->setLocation(readSourceLocation());
E->SourceTy = Record.readType();
E->Index = Record.readExpr();
E->setType(E->SourceTy);
}

void ASTStmtReader::VisitSYCLBuiltinNumBasesExpr(SYCLBuiltinNumBasesExpr *E) {
Expand All @@ -1823,6 +1824,7 @@ void ASTStmtReader::VisitSYCLBuiltinBaseTypeExpr(SYCLBuiltinBaseTypeExpr *E) {
E->setLocation(readSourceLocation());
E->SourceTy = Record.readType();
E->Index = Record.readExpr();
E->setType(E->SourceTy);
}

void ASTStmtReader::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/module.modulemap.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ module LLVM_Config_Config {
header "llvm/Config/llvm-config.h"
export *
}

module SYCL {
header "sycl/sycl.hpp"
export *
}
2 changes: 2 additions & 0 deletions sycl/include/sycl/ext/intel/esimd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
#pragma clang diagnostic ignored "-Wpsabi"
#endif // !defined(__SYCL_DEVICE_ONLY__) && defined(__clang__)

#include <sycl/sycl.hpp>

#include <sycl/detail/type_traits.hpp>
#include <sycl/ext/intel/esimd/alt_ui.hpp>
#include <sycl/ext/intel/esimd/common.hpp>
Expand Down
3 changes: 3 additions & 0 deletions sycl/include/sycl/ext/intel/fpga_extensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/sycl.hpp>

#include <sycl/ext/intel/experimental/fpga_lsu.hpp>
#include <sycl/ext/intel/experimental/pipes.hpp>
#include <sycl/ext/intel/fpga_device_selector.hpp>
Expand Down
7 changes: 7 additions & 0 deletions sycl/include/sycl/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#pragma once

#ifdef HAHA
int foo() { return 33; }
#endif

#if 0

#include <sycl/accessor.hpp>
#include <sycl/aspects.hpp>
#include <sycl/atomic.hpp>
Expand Down Expand Up @@ -86,3 +92,4 @@
#include <sycl/ext/oneapi/sub_group.hpp>
#include <sycl/ext/oneapi/sub_group_mask.hpp>
#include <sycl/ext/oneapi/weak_object.hpp>
#endif
2 changes: 2 additions & 0 deletions sycl/test/extensions/fpga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <type_traits>

#if 1
namespace intelfpga {
template <unsigned ID> struct ethernet_pipe_id {
static constexpr unsigned id = ID;
Expand Down Expand Up @@ -163,3 +164,4 @@ int main() {

return 0;
}
#endif
Loading