Commit graph

124 Commits

Autor SHA1 Nachricht Datum
Evgeny Mankov 3233d633da [HIPIFY] Remove cudaBuiltin matcher
[ROCm/hip commit: 5d92a6c252]
2017-12-06 20:22:14 +03:00
Evgeny Mankov eefead5a1c [HIPIFY] Disable cudaBuiltin matcher.
As HIP has started to support vanilla CUDA syntax for threadIdx, blockIdx, blockDim and gridDim.
Other CUDA builtins are not tracked for now.


[ROCm/hip commit: 71d2fb20c8]
2017-12-05 20:28:51 +03:00
Evgeny Mankov af07df0b85 [HIPIFY] remove duplicates from CUDA_IDENTIFIER_MAP
[ROCm/hip commit: f24dfc6f36]
2017-12-05 19:46:53 +03:00
Evgeny Mankov 499e247586 Merge pull request #262 from ChrisKitching/frontendaction
[HIPIFY] Mostly fix preprocessor-or-template induced issues

[ROCm/hip commit: aa05b3d84e]
2017-11-27 17:30:11 +03:00
Chris Kitching 096136750c Use proper clang diagnostics for printing warnings
Much pretty. Very wow

This gives users all the usual power when it comes to manipulating
clang diagnostics. People can pass -Werror can have hipify fail if
it doesn't completely translate a file, for example. Much nicer
than reinventing the wheel.


[ROCm/hip commit: 6b767a59ba]
2017-11-13 20:58:55 +00:00
Chris Kitching 78cf713140 Use a custom FrontendAction to simplify identifier translation
Most of what hipify does is really just replacing CUDA idenitifers
with HIP ones. CUDA function calls, preprocessor macro calls,
enum references, types, etc.

This is problematic: calls/types/enum-refs require name resolution
for the AST matcher to work. This fails in the presence of code
deleted by the preprocessor, and in two-pass template compilation.

Instead, we can simply hook the lexer and have it rewrite the
identifiers for us.

This approach means identifier transformations will work correctly
regardless of where they appear (and we get to delete lots of code)

- Fixes #260
- Helps a bit with #207 - it will still fail to translate kernel
calls in preprocessor-ignored code, but everything except kerel
launches should translate correctly now, even in
preprocessor-deleted code.


[ROCm/hip commit: 24cdc5e1d3]
2017-11-13 20:58:54 +00:00
Chris Kitching c2d54f0154 Add hipify mappings for all CUDA headers that have HIP equivalents
I'm particularly running into issues with `device_types.h` in real
CUDA code...


[ROCm/hip commit: 23b5d26582]
2017-11-13 17:20:07 +00:00
Evgeny Mankov 27c5e94c81 [HIPIFY] fix typo - missing )
[ROCm/hip commit: 44c74b6511]
2017-10-27 23:31:43 +03:00
Chris Kitching 54f786583b Remove commented else-block
A warning statement for _string literals_ seems a bit unhelpful.
There's no value in this being here.


[ROCm/hip commit: 20871a3a07]
2017-10-27 20:12:33 +01:00
Chris Kitching 05699e3779 Decouple the statistics system from the code translation
The original implementation had the statistics system woken very
tightly into things like PPCallbacks, with counters duplicated
in two places, and all the output code duplicated. This made it
very difficult to alter the structure of the program without
breaking the statistics system.

Since the planned approach for solving the remaining preprocessor
bugs needs the introduction of a custom FrontendAction, and such
a restructure was incompatible with the way the statistics system
was set up, this rewrite was required.

'tis rather simpler now, mind you :D

This commit also fixes an issue where some stats were counted
twice, and allows `-print-stats` to operate independently of
`-stat-output`, allowing you to print stats to a file without
printing them to a terminal (or vice-versa).


[ROCm/hip commit: b303ffe53e]
2017-10-27 20:12:33 +01:00
Chris Kitching 1454bf9651 Copy-paste less in the statistics printing code
[ROCm/hip commit: 5699c18adc]
2017-10-27 20:12:33 +01:00
Chris Kitching e4569bc84e Inline updateCountersExt
[ROCm/hip commit: 50448aec3b]
2017-10-27 20:12:32 +01:00
Chris Kitching a3c1d30745 Update counter maps sanely
operator[] default-constructs the map value if no value exists
for that key. Default-construction of int yields a zero. So all
the manual faffing around is just unnecessary.


[ROCm/hip commit: d8beee8918]
2017-10-27 20:12:32 +01:00
Chris Kitching 51df5b20c9 Prefer references to pointers in updateCountersExt()
[ROCm/hip commit: 00bb447e55]
2017-10-27 20:12:32 +01:00
Chris Kitching 25517e41fd Move string utility functions into their own translation unit
[ROCm/hip commit: ee8e11a720]
2017-10-27 20:12:32 +01:00
Chris Kitching 85fd2e6f51 Extract LLVM compatibility code into its own translation unit
[ROCm/hip commit: 1bd837b4b1]
2017-10-27 20:12:32 +01:00
Chris Kitching 389fa2e68e Remove unused field
[ROCm/hip commit: 0c09bdf523]
2017-10-27 20:12:32 +01:00
Chris Kitching 48e7403762 Remove CUDA_EXCLUDES
An artefact from a now-defunct hack to avoid corrupting programs


[ROCm/hip commit: c6707ef33c]
2017-10-27 20:12:32 +01:00
Chris Kitching 199d75adc0 Make unsupported actually be a bool...
[ROCm/hip commit: 2f376c9b25]
2017-10-27 20:12:31 +01:00
Evgeny Mankov a9c3228cb1 Merge pull request #234 from ChrisKitching/warningSpam
[HIPIFY] Do not process __fetch_builtin_* in cudaCall()

[ROCm/hip commit: 9151a355c6]
2017-10-27 21:30:42 +03:00
Chris Kitching c15f6bdf5c Greatly enhance handling of macros in kernel launches
All but the most contrived use of macros is now properly handled -
have a look at the new testcases this commit adds. You can have
macros in kernel calls, macros spanning chunks of your arguments,
the call, call parameters, or callee can all be macros or
partially macros.


[ROCm/hip commit: 094b2b9b05]
2017-10-26 17:28:46 +01:00
Chris Kitching d0acfd5bde Simplify how kernel launch expressions get translated
It seems like there was a lot of machinery here that is no longer
needed now we have hipLaunchKernelGGL (which doesn't require us
to insert an extra argument into kernel functions). We no longer
need to waste cycles scanning the AST for callees.

We can literally just do "Take the callee expression, and dump
it into the first argument of hipLaunchKernelGGL()".


[ROCm/hip commit: eff86d975b]
2017-10-26 17:28:30 +01:00
Chris Kitching 59713c2459 Deduplicate preprocessor code
There's three functions here that all do the same thing...

There was also logic that looks for numeric literals and works
backwards to find the macro name from which they are expanded.
I previously introduced code that rewrites macro references at
expand-time in the `MacroExpands` callback, so that code is no
longer doing anything useful.


[ROCm/hip commit: fd911e1839]
2017-10-26 17:28:30 +01:00
Chris Kitching ca913bb196 Rewrite _all_ CUDA macro identifiers in the preprocessor
Calls to macros that were themselves CUDA API calls were often
being missed - this applies the identifier transform to macro
names at the callsites, too.


[ROCm/hip commit: d1e26b2e7e]
2017-10-26 17:27:56 +01:00
Chris Kitching cbf786a8fd Don't special-case source locations for calls in macros
The source location for a call that's inside a macro body will,
by default, point into the macro definition itself. The original
logic was causing macro invocations to be overwritten, as I
explain here:
https://github.com/ROCm-Developer-Tools/HIP/issues/207#issuecomment-337521851

The existing PPCallbacks code is correctly rewriting macro
definitions, so the practical effect of this change is that AST
rewrites on code that's expanded from macros are no-ops.

It might be a performance optimisation to put a short-circiut at
the top of the AST callbacks to abort when faced with code that
was expanded from macros.

It might yet prove wise to do absolutely everything at lex-time...


[ROCm/hip commit: 4a794ed8c0]
2017-10-26 17:26:37 +01:00
Chris Kitching 32cbe68a93 Prefer early-return to deep nesting
A chain of 7 closing braces is never a great sign :D

In the process it became apparant that the unsupported flag
was being silently ignored, causing users to be left with cuda
API calls in their programs with no warning given. This has been
rectified for consistency.


[ROCm/hip commit: 35a892bc77]
2017-10-26 17:26:37 +01:00
Chris Kitching 78826f5512 Do not process __fetch_builtin_* in cudaCall()
Fixes #205


[ROCm/hip commit: 2a5acac80e]
2017-10-26 17:23:55 +01:00
Chris Kitching 5092f3f184 Refactor cudaCall to prefer early return to deep nesting
Sorry for the invasive refactor, but this was making reasoning
about this function more difficult.


[ROCm/hip commit: 778d6827f9]
2017-10-24 20:38:49 +01:00
Chris Kitching 22e7c4ebfc Split the giant lookup table into 3 smaller ones
Instead of having a single, enormous LUT for all CUDA names, let's
have separate ones for different types of entity. We often know
that we're looking at a typename, or a function name, or a macro
name - so we can be more efficient (and resilient to name
collisions) by having smaller lookup tables for each of those
classes of entity).

Here we start that off by having three LUTs:
- Header names
- Type names
- Everything else

Future work could usefully split "everything else" into:
- enum values
- macro names
- function names
- everything else

It's worth noting that the "needs new matcher" todos I delete here
were actually resolved with the previous commit. It no longer
naively searches for things that start with "cu*" - it will find
exactly those things that are present in our lookup tables.


[ROCm/hip commit: 695a1eb059]
2017-10-24 20:38:49 +01:00
Chris Kitching 1cf0c75c49 One matcher for type expressions to rule them all
Previously, there were different AST matchers for each
language construct that contains a type reference, and custom
logic to perform the transformation within each of those
structures.

Since the transformation in all such cases was only replacing
CUDA types with hip ones, we can instead use an AST matcher
that finds and updates the type references directly.
This simplifies the program considerably, and it won't fail
when it finds a language feature (or complicated type expression)
that nobody wrote custom logic for yet.


[ROCm/hip commit: a1d8340314]
2017-10-24 20:38:49 +01:00
Chris Kitching 0c0ca79c1c Make control flow less insane
`while(false)` is certainly a bold choice.


[ROCm/hip commit: 182d346356]
2017-10-24 20:38:49 +01:00
Chris Kitching 9eff10c00d Move giant lookup table into another translation unit
Also, rewrote it as a constant variable instead of a function
that imperatively fills a map. It's shorter, faster the compile,
and (depending on how badly the compiler screws it up) maybe
faster to run.

And, of course, it starts breaking up that giant .cpp file.


[ROCm/hip commit: 4ab29113a1]
2017-10-24 20:38:25 +01:00
Chris Kitching 71cc7740ff Tweak some version numbers in clang version compatibility checks
Apparently a couple of those APIs changed in clang 5, not 4.

Drat.


[ROCm/hip commit: 0207a29f78]
2017-10-24 01:45:23 +01:00
Chris Kitching ecbe19828e Avoid a double-free of HipifyPPCallbacks instance
This bug was present all along, but something changed in the order
of de-initialisation performed by llvm that makes it actually
crash now.

The constructor of HipifyPPCallbacks gives:
```
std::unique_ptr<HipifyPPCallbacks>(this)
```
to the LLVM Preprocessor instance. The Preprocessor instance
subsequently frees the HipifyPPCallbacks, which is then freed
again when we leave the stack frame at line 4340.

So: let's leak the HipifyPPCallbacks onto the heap, and leave
the LLVM Preprocessor object responsible for tidying it up.


[ROCm/hip commit: f5b273fc4f]
2017-10-19 17:09:32 +01:00
Chris Kitching dc869ef474 Be agnostic to the new getReplacements() API
See comment


[ROCm/hip commit: 6718519025]
2017-10-19 17:08:55 +01:00
Chris Kitching c08f105982 Use inline initialisers to set default field values
A trivial cleanup that helps in a moment..


[ROCm/hip commit: 24fc459f69]
2017-10-19 17:08:55 +01:00
Chris Kitching 2584387bfc Omit now-removed Filename string arg from handleBeginSource
[ROCm/hip commit: 7ae6a10c99]
2017-10-19 17:08:55 +01:00
Chris Kitching b2ac8756fc Cope with Replacements now having llvm::Error returns
[ROCm/hip commit: 7e253365f1]
2017-10-19 17:08:54 +01:00
Chris Kitching 5e45fd1c7c Cope with clang 4.0's rename of getNumArgs()
Sorry, this one I couldn't do in a perfectly elegant way ;)


[ROCm/hip commit: 575bedb28c]
2017-10-19 17:08:54 +01:00
Chris Kitching 459b78e997 Remove unnecessary call to Retain() on a smart pointer
The Preprocessor smart pointer is held by the CompilerInstance,
and therefore its reference count cannot reach zero until the
CompilerInstance itself is destroyed.

If the CompilerInstance is destroyed, you have more to worry about
than just the preprocessor being deallocated!

Newer versions of the LLVM/Clang API migrated to using
std::shared_ptr, so there is no `Retain()` function (by that
name, anyway). Eliminating this redundant use is a neat and
backward-compatible way to become compatible with newer versions
of the LLVM/Clang API.


[ROCm/hip commit: 0953a7887d]
2017-10-19 17:08:54 +01:00
Chris Kitching 316faf5fc7 Refer to clang::StringLiteral explicitly
Newer versions of llvm/clang mean there is both an
llvm::StringLiteral and a clang::StringLiteral. Since we're
dumping both namespaces wholesale into the global namespace with
`using` declarations, this creates a name collision, which must be
resolved.

This change is backwards-compatible, and fixes a problem you
encounter when using newer versions of the llvm/clang API.


[ROCm/hip commit: 2975d00edc]
2017-10-19 17:08:54 +01:00
Chris Kitching 24adac896b Fix two faulty LLVM version checks
What we actually want to do here is use the StringRef version in
versions newer than 3.8, and the void one in 3.8 and older.

Checking "major-version >= 3 && minor-version >= 9" does not do
what we want. Consider what this will do for version 4.0, for
which minor-version is zero...


[ROCm/hip commit: 3a2fe40f78]
2017-10-19 17:08:54 +01:00
Chris Kitching 9cd878f5c7 Inline a redundant std::vector
Looks like a partial refactor..?


[ROCm/hip commit: 4ba2b2e5b8]
2017-10-19 14:11:25 +01:00
Chris Kitching 0602fc6124 Allow user-provided -std=* argument to override the default
This means any user-provided -std=* argument overrides this one,
allowing use of newer standards.


[ROCm/hip commit: 742526706b]
2017-10-19 14:11:15 +01:00
Evgeny Mankov 65497bfaff Merge pull request #210 from ChrisKitching/output-hipify
[HIPIFY] Alter output file behaviour slightly

[ROCm/hip commit: db7661ccad]
2017-10-18 21:46:31 +03:00
Chris Kitching 49f9c4feff Unconditionally append .hip as the default output filename
[ROCm/hip commit: 296c5a33ce]
2017-10-18 19:19:18 +01:00
Chris Kitching a2629da99c If an output path is given _use it_
Don't append .hip to a user-provided output file...


[ROCm/hip commit: e4e17a56bb]
2017-10-16 17:32:57 +01:00
Chris Kitching 0c0891836f Operate on a temp file in-place
This copies to the output after operation, instead of working
_on_ the output. This allows includes to work correctly, while
supporting output paths anywhere on the filesystem.

Fixes #208
Fixes #206


[ROCm/hip commit: 2263cb9f72]
2017-10-16 17:32:56 +01:00
Evgeny Mankov 0162211c28 [HIPIFY][#199][Partial fix] Fix for cudaLaunchKernel transformation
[Synopsis]
If any of kernel arguments is a MACRO its location calculation is wrong (location of its definition is actually calculated).
Thus garbage code is being produced on the place of such a MACRO starting from the end of its actual definition.

[Solution]
Add isMacroBodyExpansion and isMacroArgExpansion checks on kernel arguments.


[ROCm/hip commit: 7ee8e2d51b]
2017-10-12 20:05:42 +03:00
Evgeny Mankov 643eb7ace7 [HIPIFY] CUDA RT Textures and Arrays support update
[ROCm/hip commit: 53ce40ebab]
2017-10-05 16:38:36 +03:00