This is an archive of the discontinued LLVM Phabricator instance.

[Polly] In getBuild() use isl::noexception bindings [NFC]
Needs ReviewPublic

Authored by refactormyself on Apr 13 2021, 5:26 AM.

Details

Summary
IslAstInfo::getBuild() still uses the ISL's pure C objects as
return values and parameter. The C++ bindings in isl_noexception
(isl::ast_build and isl::ast_node) provides better pointer management

 - Replace the pure C objects with their C++ bindings
 - Refactor calls to this function within IslNodeBuilder.cpp

No functional changes were made.

Diff Detail

Event Timeline

refactormyself created this revision.Apr 13 2021, 5:26 AM
refactormyself requested review of this revision.Apr 13 2021, 5:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2021, 5:26 AM

Always use isl::manage() and isl::manage_copy() when constructing a C++ isl object from a raw C pointer

polly/lib/CodeGen/IslAst.cpp
660

You should use isl::manage() to initialize a isl::ast_build object. The constructor you are using is a private method

refactormyself added a reviewer: patacca.
  • use isl::manage() to initialize a isl::ast_build object
  • use isl::ast_build() since it's equivalent to isl::ast_build(nullptr)
  • use auto

This is the declaration of the function you should use (you can find it in isl-noexceptions.h):
ast_build manage(__isl_take isl_ast_build *ptr);

When creating a isl C++ object from a raw C pointer you should never call the constructor because it's declared as private. You should instead call isl::manage or isl::manage_copy like this:

ast_build BuildCopied = isl::manage_copy(raw_c_pointer);
ast_build Build = isl::manage(raw_c_pointer);

Also you should probably fix the code styling with ninja polly-update-format or ninja polly-check-format

polly/lib/CodeGen/IslAst.cpp
659

That's not what I meant. You should use isl::manage without calling directly the constructor.

  • call isl::manage() directly instead of the copy constructor- isl::ast_build()
  • explicitly declare auto * variables

Thank you for the patch and thanks @patacca for helping with the review.

Could you add NFC to the patch title. It signifies it is a refactoring. (I prefer ir appended as NFC. to the end of the title)

Could you upload the patch with context? See https://www.llvm.org/docs/Phabricator.html#phabricator-request-review-web for details (tldr: Add -U999999 to git diff)

The LLVM coding prefers not using auto except in specific cases. The current code base may not adhere to this everywhere, but the style could be made more conformant when making changes.

The harbormaster build fails indicate that there is some memory corruption going on. At a first glance I suspect it is the return value of IslAstInfo::getBuild. Line 722 of IslAst.cpp calls isl::manage_copy on it, which in case of __isl_give would usually be memory leak. The __isl_give might not actually be correct, repectively IslAstInfo::getBuild should have made a copy before returning.

Thank you @Meinersbur and @patacca for your reviews. I will make amendments.
@Meinersbur please, I will appreciate some clarification here:

The harbormaster build fails indicate that there is some memory corruption going on. At a first glance I suspect it is the return value of IslAstInfo::getBuild. Line 722 of IslAst.cpp calls isl::manage_copy on it, which in case of __isl_give would usually be memory leak. The __isl_give might not actually be correct, repectively IslAstInfo::getBuild should have made a copy before returning.

I am trying to understand what is meant by "The __isl_give might not actually be correct ...".
Here is the line of I think is being referred to:

isl::ast_build Build = IslAstInfo::getBuild(isl::manage_copy(Node));

If I understand the code, isl::manage_copy should raise an exception if Node is not "correct"

Do you mean calling isl::manage_copy and not isl::manage on Payload->Build , so it checks and raises an exception?

I am trying to understand what is meant by "The __isl_give might not actually be correct ...".
Here is the line of I think is being referred to:

isl::ast_build Build = IslAstInfo::getBuild(isl::manage_copy(Node));

The __isl_give annotation means that getBuild returns an abject for which the caller must assume the ownership of (ie. has the responsibility to eventually free it). isl::manage_copy(IslAstInfo::getBuild(Node)) Makes a copy of the object returned, so that there are 2 objects. The copied object will eventually be freed by the dtor, but the object returned itself is not.

isl::manage_copy(Node) does NOT make a copy of the payload that is returned by getBuild.

If I understand the code, isl::manage_copy should raise an exception if Node is not "correct"

Exceptions are disabled in LLVM. There are some assertions in debug builds, but they don't catch everything.

Do you mean calling isl::manage_copy and not isl::manage on Payload->Build , so it checks and raises an exception?

Moving the isl::manage_copy from the caller of getBuild into the method itself.

refactormyself retitled this revision from [Polly] In getBuild() use isl::noexception bindings to [Polly] In getBuild() use isl::noexception bindings [NFC].
  • return a copy of the build from within getBuild()
  • do not copy the Node passed into getBuild()
Meinersbur added inline comments.Apr 21 2021, 10:59 AM
polly/lib/CodeGen/IslAst.cpp
711

I think this needs to be isl::manage_copy as the previous annotation for the argument was __isl_keep.

polly/lib/CodeGen/IslNodeBuilder.cpp
856–858

I think this needs to be isl::manage_copy as the previous annotation for the argument was __isl_keep.