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

Add a test dxil lib for testing loadability #5952

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if (WIN32)
# This target can currently only be built on Windows.
add_llvm_tool_subdirectory(dxexp)
endif (WIN32)
add_llvm_tool_subdirectory(testdxildll)
# HLSL Change ends

# add_llvm_tool_subdirectory(llc) # HLSL Change
Expand Down
34 changes: 34 additions & 0 deletions tools/testdxildll/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Builds a testing version of dxil.dll

set(SHARED_LIBRARY TRUE)

set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
BitWriter
DxcSupport
DxilContainer
DxilRootSignature
HLSL
MSSupport
Support
TransformUtils
)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${LLVM_BINARY_DIR}/projects/include
)

set(SOURCES
DxilLib.cpp
DxilLib.def
DxilContainerBuilder.cpp
DxilValidator.cpp
)

add_llvm_library(testdxil SHARED ${SOURCES})

if(WIN32)
add_dependencies(testdxil DxcRuntimeEtw)
endif(WIN32)

33 changes: 33 additions & 0 deletions tools/testdxildll/DxilContainerBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
///////////////////////////////////////////////////////////////////////////////
// //
// dxcontainerbuilder.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Implements the Dxil Container Builder //
// //
Copy link
Member

@hekota hekota Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incorrect, it does not really implement a container builder. Any reason why this is not part of the DxilLib.cpp?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The reason it's not part of DxilLib.cpp is because of some inclusions this requires that confuse the code in DxilLib.cpp. I spent a little while trying to resolve them, but decided it wasn't worth it as this is the structure of similar code elsewhere likely for the same reason.

///////////////////////////////////////////////////////////////////////////////

#include "dxc/DxilContainer/DxcContainerBuilder.h"
#include "dxc/DxilContainer/DxilContainer.h"
#include "dxc/Support/ErrorCodes.h"
#include "dxc/Support/FileIOHelper.h"
#include "dxc/Support/Global.h"
#include "dxc/Support/WinIncludes.h"
#include "dxc/Support/dxcapi.impl.h"
#include "dxc/dxcapi.h"

#include <algorithm>

using namespace hlsl;

HRESULT CreateDxcContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be implemented in DxilLib.cpp, since we really aren't supplying another implementation here? One might almost mix it up with DxcContainerBuilder.cpp where we implement the internal builder that this code uses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some compilation errors when I tried to do this before that had to do with conflicting defines. I'll take another run at it.

// Call dxil.dll's containerbuilder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment accurate? Isn't this creating the local container builder instead?

*ppv = nullptr;
CComPtr<DxcContainerBuilder> Result(
DxcContainerBuilder::Alloc(DxcGetThreadMallocNoRef()));
IFROOM(Result.p);
Result->Init();
return Result->QueryInterface(riid, ppv);
}
150 changes: 150 additions & 0 deletions tools/testdxildll/DxilLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
///////////////////////////////////////////////////////////////////////////////
Copy link
Member

@hekota hekota Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind renaming DxilLib.* files to TestDxilLib.*? I am often searching through the whole code base and since this code is similar to the actual DxilLib.cpp it would be nice to have it under a different name so it can be clearly seen in the search results that this is the test dll.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

// //
// DxilLib.cpp //
// Copyright (C) Microsoft. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Implements the DxcCreateInstance, DxcCreateInstance2, and DllMain //
// functions for the dxil library module //
// //
///////////////////////////////////////////////////////////////////////////////

#include "dxc/Support/Global.h"
#include "dxc/Support/WinIncludes.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include <algorithm>
#ifdef _WIN32
#include "Tracing/DxcRuntimeEtw.h"
#include "dxc/Tracing/dxcetw.h"
#endif

#ifdef _WIN32
#define DXC_API_IMPORT
#else
#define DXC_API_IMPORT __attribute__((visibility("default")))
#endif

#include "dxc/dxcisense.h"
#include "dxc/dxctools.h"

HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv);
HRESULT CreateDxcContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv);

// C++ exception specification ignored except to indicate a function is not
// __declspec(nothrow)
static HRESULT InitMaybeFail() throw() {
HRESULT hr;
bool memSetup = false;
IFC(DxcInitThreadMalloc());
DxcSetThreadMallocToDefault();
memSetup = true;
if (::llvm::sys::fs::SetupPerThreadFileSystem()) {
hr = E_FAIL;
goto Cleanup;
}
Cleanup:
if (FAILED(hr)) {
if (memSetup) {
DxcClearThreadMalloc();
DxcCleanupThreadMalloc();
}
} else {
DxcClearThreadMalloc();
}
return hr;
}

#if defined(LLVM_ON_UNIX)
HRESULT __attribute__((constructor)) DllMain() { return InitMaybeFail(); }

void __attribute__((destructor)) DllShutdown() {
DxcSetThreadMallocToDefault();
::llvm::sys::fs::CleanupPerThreadFileSystem();
::llvm::llvm_shutdown();
DxcClearThreadMalloc();
DxcCleanupThreadMalloc();
}
#else

#pragma warning(disable : 4290)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) {
if (Reason == DLL_PROCESS_ATTACH) {
EventRegisterMicrosoft_Windows_DxcRuntime_API();
DxcRuntimeEtw_DxcRuntimeInitialization_Start();
HRESULT hr = InitMaybeFail();
DxcRuntimeEtw_DxcRuntimeInitialization_Stop(hr);
if (FAILED(hr)) {
EventUnregisterMicrosoft_Windows_DxcRuntime_API();
return hr;
}
} else if (Reason == DLL_PROCESS_DETACH) {
DxcRuntimeEtw_DxcRuntimeShutdown_Start();
DxcSetThreadMallocToDefault();
::llvm::sys::fs::CleanupPerThreadFileSystem();
::llvm::llvm_shutdown();
DxcClearThreadMalloc();
DxcCleanupThreadMalloc();
DxcRuntimeEtw_DxcRuntimeShutdown_Stop(S_OK);
EventUnregisterMicrosoft_Windows_DxcRuntime_API();
}

return TRUE;
}

void *__CRTDECL operator new(std::size_t size) {
void *ptr = DxcNew(size);
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
void *__CRTDECL operator new(std::size_t size,
const std::nothrow_t &nothrow_value) throw() {
return DxcNew(size);
}
void __CRTDECL operator delete(void *ptr) throw() { DxcDelete(ptr); }
void __CRTDECL operator delete(void *ptr,
const std::nothrow_t &nothrow_constant) throw() {
DxcDelete(ptr);
}
#endif

static HRESULT ThreadMallocDxcCreateInstance(_In_ REFCLSID rclsid,
_In_ REFIID riid,
_Out_ LPVOID *ppv) {
*ppv = nullptr;
if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) {
return CreateDxcValidator(riid, ppv);
}
if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) {
return CreateDxcContainerBuilder(riid, ppv);
}
return REGDB_E_CLASSNOTREG;
}

DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(_In_ REFCLSID rclsid,
_In_ REFIID riid,
_Out_ LPVOID *ppv) {
HRESULT hr = S_OK;
DxcEtw_DXCompilerCreateInstance_Start();
DxcThreadMalloc TM(nullptr);
hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv);
DxcEtw_DXCompilerCreateInstance_Stop(hr);
return hr;
}

DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(_In_ IMalloc *pMalloc,
_In_ REFCLSID rclsid,
_In_ REFIID riid,
_Out_ LPVOID *ppv) {
if (ppv == nullptr) {
return E_POINTER;
}
HRESULT hr = S_OK;
DxcEtw_DXCompilerCreateInstance_Start();
DxcThreadMalloc TM(pMalloc);
hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv);
DxcEtw_DXCompilerCreateInstance_Stop(hr);
return hr;
}
5 changes: 5 additions & 0 deletions tools/testdxildll/DxilLib.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LIBRARY testdxil

EXPORTS
DxcCreateInstance
DxcCreateInstance2
Loading