Skip to content

[Linux-port] Working Interface IIDs for Linux #1342

Closed

Description

@ehsannas

For full description of what we are trying to achieve, please see issue #1341

The purpose of this issue to describe one possible solution to handling Interface IIDs throughout the codebase in order to make it possible for DXC's COM-related code to compile and run successfully on Linux.

Background

Unique IIDs (Object runtime type information)

A COM object exposes its features through an interface, which is a collection of member functions.

In order to use interfaces, we need to uniquely identify them, and also be able to invoke methods of each interface.

Every interface has its own unique interface identifier, named an IID, which eliminates collisions that could occur with human-readable names.

A COM class is identified by using a unique 128-bit Class ID (CLSID) that associates a class with a particular deployment in the file system

Interfaces defined in the code base specify GUIDs such as:
https://github.com/Microsoft/DirectXShaderCompiler/blob/f8fa9c92a073aadec2720e17154f58d39ac22cf9/include/dxc/dxcapi.h#L146-L147

We need to find an alternative solution for these IDs.

QueryInterface

The IIDs described above are used (and passed around) throughout the code base. One of their most important usages is to enable the QueryInterface mechanism.

The QueryInterface member function provides polymorphism for COM. Call QueryInterface to determine at run time whether a COM object supports a particular interface. The COM object returns an interface pointer in the ppvObjectout parameter if it implements the requested interface, otherwise it returns NULL. The QueryInterface member function enables navigation among all of the interfaces that a COM object supports.

In order for QueryInterface to work, it needs to be able to compare the IID of different interfaces. It does so using the __uuidof(NAME) operator. This operator is also Microsoft-specific, therefore our solution should be able to replace this mechanism with an equivalent that can work on Linux.

Alternative Solutions

Using Run-time Type Information

Using runtime type information could be one possible solution. In this solution, one can define the following:

#define uuid(guid_str)
#define __uuidof(T) std::type_index(typeid(T))
#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), reinterpret_cast<void**>(ppType)
typedef std::type_index REFIID;

The first line ignores all the uuid("...") defined for interfaces in the code, and the second line will use the standard std::type_index and the C++ typeid operator to get the unique identifier for a type. Then, the QueryInterface methods can compare the typeid of different interfaces in order to perform their operations.

While this is a working solution, it is far from ideal. RTTI is optional with some compilers, and may not be available. RTTI also adds a certain overhead to the executable. You can also find further guidance/discussion here.

Using static member

This is a solution that is also used by the LLVM code base. The idea is to add an insignificant static private member to each interface, and given that the address of such member is always unique for all instances of the interface, we can use that address as the unique identifier.

typedef const void *REFIID;

#define DECLARE_CROSS_PLATFORM_UUIDOF(T)                                       \
public:                                                                        \
  static REFIID uuidof() { return static_cast<REFIID>(&T##_ID); }              \
private:                                                                       \
  static const char T##_ID;

+#define DEFINE_CROSS_PLATFORM_UUIDOF(T) const char T::T##_ID = '\0';

+#define __uuidof(T) T::uuidof()

With this adapter, IsEqualIID will only have to perform a pointer comparison between two void*s, and QueryInterface works as expected. It is worth mentioning that IID_PPV_ARGS works well with this solution as well.

#define IID_PPV_ARGS(ppType) (**(ppType)).uuidof(), reinterpret_cast<void**>(ppType)

Usage

We have implemented both of these solutions and seen them work in action. But as stated above, the latter is the superior solution. The adapter code above can be easily used to turn any interface into a cross-platform working interface. What needs to be done is to add the DECLARE_CROSS_PLATFORM_UUIDOF to the end of the intended interface, and add the DEFINE_CROSS_PLATFORM_UUIDOF to the cpp that implements the interface.

For example:
In header file:

struct __declspec(uuid("7241d424-2646-4191-97c0-98e96e42fc68"))
IDxcBlobEncoding : public IDxcBlob {
  ...
  ...
  DECLARE_CROSS_PLATFORM_UUIDOF(IDxcBlobEncoding)
};

struct __declspec(uuid("e5204dc7-d18c-4c3c-bdfb-851673980fe7"))
IDxcLibrary : public IUnknown {
  ...
  DECLARE_CROSS_PLATFORM_UUIDOF(IDxcLibrary)
};

In cpp file:

DEFINE_CROSS_PLATFORM_UUIDOF(IDxcBlobEncoding) 
DEFINE_CROSS_PLATFORM_UUIDOF(IDxcLibrary)
...

Activity

  1. added
    linux
    on Jun 4, 2018
  2. marcelolr commented on Jun 5, 2018

    @marcelolr
    Contributor

    The way this worked prior to __uuidof was to have the IID GUIDs be available as structs (we definitely still do that for CLSID_ values). Callers can specific the IID (which is a bit clumsier but I don't think would be too terrible). This keeps the same binary interface everywhere, and is needed to interop better with things like Mono.

  3. ehsannas commented on Jun 5, 2018

    @ehsannas
    ContributorAuthor

    Marcelo Lopez Ruiz (@marcelolr) Thanks for your suggestion. I had briefly thought about your suggestion in the past but didn't pursue it because of number of usages of __uuidof and also I was wondering how that would work in the recursive QueryInterface functions? e.g. what can we use instead of __uuidof(TInterface) in Line 153 below?

    https://github.com/Microsoft/DirectXShaderCompiler/blob/b9f2c78314d20a4749f2902c91ca967c3f8d4963/include/dxc/Support/microcom.h#L150-L159

    Thanks

  4. marcelolr commented on Jun 7, 2018

    @marcelolr
    Contributor

    Summoning Tex Riddell (@tex3d) … We used to have a bunch of overloads with a number of identifiers before without the recursion. Thoughts?

  5. pow2clk commented on Jun 9, 2018

    @pow2clk
    Collaborator

    I may have discovered some information relevant to this discussion.

    Currently, there's a pretty substantial difference between the way the WIndows and Linux builds handle dynamic libraries. Well, library really (dxcompiler). On Windows, it is loaded explicitly with the platform relevant methods, on Linux and others, it is dynamically linked, requiring no such explicit loading. The code that would do this loading and initialize the library as a separate entity is ifdefed out.

    Because I've been bitten by this discrepancy before and been forced to dramatically alter my approach because of surprise Windows failures, I've been experimenting with moving Linux to explicitly load dxcompiler. I had an old hacky Linux port that did this, but I hadn't dealt properly with the QueryInterface problem at all. In my new endeavor, I'm running into a problem with the static uuids because dxc and dxcompiler both need access to some of the same classes so each one creates its own static member and they obviously don't match. So interface searches fail. The only solution I can think of right now is to have dxc query the value from dxcompiler, which is possible, albeit inelegant.

    It's possible I'm missing something. It's possible that no one but me thinks that the non-windows builds should explicitly load their dynamic libraries and this point is moot. Either way, this might be why the approach was rejected on Windows. I'll leave whether that means it should be rejected for the Linux build to those who are getting paid to do this. 😄

  6. pow2clk commented on Jun 9, 2018

    @pow2clk
    Collaborator

    To clarify my previous comment. Windows actually dynamically links and explicitly loads the dxcompiler library. . . . I don't know why. Is that a mistake? Linux build files and code would try to do the same thing, but it doesn't work as well there for a few reasons all stemming from the fact that while dynamically linked libraries on Windows hide anything not marked with dllexport, Linux exposes everything unless you specify not to. Even then, it refuses to hide some things, most notably the global operator new implementation in DXCompiler.cpp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

linux

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions